How to grep for a value after an = sign

grepregular expression

How do I specify a grep to look for all possible values

i.e., with a file like (the 9701 could be any value):

9701=1?? 
9701=10.Pp 
9701=1a 8a 
9701=3.a_tt 
9701=1/a -00
9701=Bg1998pps

I could try

egrep -Eo '9701=[A-Z]+[a-z]+[0-9]{1,50}' test.log

This only gives me Uppercase/lowercase & number values. How do I include values with special characters in the grep request? i.e., with spaces, dots, hyphens, underscores etc.

Best Answer

To include all other characters in your grep you could use this:

egrep -Eo '9701=.{1,50}' test.log

The dot represents ANY character.

But that won't cut off the "9701=" part of each line. To achieve this you could use cut

cut -d "=" -f 2- test.log

Though this would stumble if the value would include = as well.

sed would fix this for you and is ultimately the better solution for your problem:

sed -r 's/^9701=(.*)$/\1/' test.log

or

sed 's/^9701=\(.*\)$/\1/' test.log

or even

sed 's/^9701=//' test.log
Related Question