Reputation: 303
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
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
Reputation: 37404
$ grep -o "example[^ ]*" foo
example="abc"
example="123asdfadf"
Upvotes: 3