2

How to find all the words that contain a substring and display them in different lines?

I have this line:

john_ford peter_smith john_stone albert_brown john_rice john_harris lewis coll

I want to display all the names that start with john_

If I use grep -o:

echo "john_ford peter_smith john_stone albert_brown john_rice john_harris lewis coll" | grep -o john_

The result is:

john_
john_
john_
john_

but my desired output is:

john_ford
john_stone
john_rice
john_harris

How could I get it?, Is it necessary to use a tool like awk?

1
  • Post your solution as an Answer, not in the Question.
    – Barmar
    Feb 22 '17 at 22:00
0

The solution: grep -o '\b'john_'\w*'

echo "john_ford peter_smith john_stone albert_brown john_rice john_harris lewis coll" |
  grep -o '\b'john_'\w*'

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.