mb47
mb47

Reputation: 303

Finding a particular string from a file

I have a file that contains one particular string many times. How can I print all occurrences of the string on the screen along with letters following that word till the next space is encountered.

Suppose a line contained:

example="123asdfadf" foo=bar

I want to print example="123asdfadf".

I had tried using less filename | grep -i "example=*" but it was printing the complete lines in which example appeared.

Upvotes: 0

Views: 55

Answers (2)

hek2mgl
hek2mgl

Reputation: 157967

Since -o is only supported by GNU grep, a portable solution would be to use sed:

sed -n 's/.*\(example=[^[:space:]]*\).*/\1/p' file

Upvotes: 0

James Brown
James Brown

Reputation: 37404

$ grep -o "example[^ ]*" foo
example="abc"
example="123asdfadf"

Upvotes: 3

Related Questions