bash 比赛后grep

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11313021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:39:57  来源:igfitidea点击:

grep after match

linuxbashgrep

提问by Linuxstartway

I have the following lines in text file

我在文本文件中有以下几行

myname aaa age 22
age 23 myname bbb 

How can i find the word after mynameusing linux grepcommand .?

如何使用 linux grep命令找到myname后面的单词?

I want the output to be the word after myname ( aaa and bbb )

我希望输出是 myname 之后的单词( aaa 和 bbb )

回答by Prince John Wesley

$ grep -Po '(?<=myname\s)\w+' inputFile

回答by MattH

$ grep -o "myname [[:alnum:]]\+" /tmp/sample | cut -f2 -d' '
aaa
bbb

回答by rush

solution with sed:

解决方案sed

sed -n '/myname/{s/.*myname \([^ ]*\).*//;p}'