3

How can I grep a specific word in a single string that contains repetitions?

For example :

Apple_1 Apple_1_Test Juice_2 Juice_2_H

If I use grep -Eo 'Apple_1' I get two results (because of two Apple_1 in the original string)

But what if I want to grep only the perfect word Apple_1 and not Apple_1_Test or Juice_2 and not Juice_2_H??

1 Answer 1

3

Add a word boundary assertion:

grep -Eo '\bApple_1\b'
3
  • 4
    Option -w is enough: grep -Ewo 'Apple_1'
    – Costas
    Apr 2, 2015 at 20:33
  • Perfect in either way :-D Thank you very much
    – GabryRome
    Apr 2, 2015 at 20:37
  • @GabryRome If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.
    – terdon
    Apr 2, 2015 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.