如何找到所有包含子串的单词并将它们显示在不同的行中?
我有这一行:
john_ford peter_smith john_stone albert_brown john_rice john_harris lewis coll
我想显示所有以john_
如果我使用grep -o
:
echo "john_ford peter_smith john_stone albert_brown john_rice john_harris lewis coll" | grep -o john_
结果是:
john_
john_
john_
john_
但我想要的输出是:
john_ford
john_stone
john_rice
john_harris
怎么才能得到呢?,有必要使用awk之类的工具吗?
答案1
解决方案: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*'