0

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.

5
  • 3
    please provide a minimal reproducible example together with what you tried so far Commented Aug 26, 2016 at 13:13
  • grep -o "example\b[^\s]*" yourfile ? Commented Aug 26, 2016 at 13:15
  • less filename | grep -i "example=*" Commented Aug 26, 2016 at 13:18
  • @Jean-FrançoisFabre -- not working, it's returning nothing. Though, Can you please explain this, I can try with it. Edit : Sorry it worked too. I had to use "-i" to include all cases, my bad. Commented Aug 26, 2016 at 13:21
  • 1
    @fedorqui thanks for pointing out. Edited the question with what I tried. Commented Aug 26, 2016 at 13:28

2 Answers 2

3
$ grep -o "example[^ ]*" foo example="abc" example="123asdfadf" 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this worked. Used -i along with -o to include all cases. Thanks a lot.
@mb47, please check the green arrow to mark this answered.
0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.