In bash, how to retrieve string starting with a specific expression? -


given piece of file:

a=as/dsdf b=fdfsf c=vcv c=15 b=1 a=azzzz))]ee a=12 z=19 r=15 

i want retrieve parts starting a=

so output in case be:

a=as/dsdf a=azzzz))]ee a=12 

i've dived bash documentation couldn't find easy, have suggestion ?

thanks

i'm assuming want starting a= next space.

it's not supported versions of grep if have -o option easy:

grep -eo 'a=[^ ]+' file 

-o prints matching part of line. -e enables extended regular expressions, enables use + mean one or more occurrences of preceding atom*. [^ ] means character other space.

otherwise, use sed capture part you're interested in:

sed -e 's/.*(a=[^ ]+).*/\1/' file 

as last resort, if version of sed doesn't support extended regexes, should work on version:

sed 's/.*\(a=[^ ]\{1,\}\).*/\1/' file 

as rightly pointed out in comments (thanks), avoid printing lines don't match, may want use -n suppress output , add p command print lines match:

sed -ne 's/.*(a=[^ ]+).*/\1/p' file 

atom* : character class, marked sub-expression or single character.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -