bash Grep 仅在匹配后打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12682715/
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
Grep Print Only After Match
提问by user1709294
I used grepthat outputs a list like this
我使用grep它输出这样的列表
/player/ABc12
/player/ABC321
/player/EGF987
/player/egf751
However I want to only give the name of the players such ABC321, EFG987, etc...
不过,我想只给玩家这样的名称ABC321,EFG987等...
采纳答案by Tim Pote
@sputnick has the right idea with grep, and something like that would actually be my preferred solution. I personally immediately thought of a positive lookbehind:
@sputnick 有正确的想法grep,而类似的东西实际上是我的首选解决方案。我个人立即想到了积极的回顾:
grep -oP '(?<=/player/)\w+' file
But the \Kworks perfectly fine as well.
但\K效果也很好。
An alternative (somewhat shorter) solution is with sed:
另一种(稍短)的解决方案是sed:
sed 's:.*/::' file
回答by Gilles Quenot
Start using grep:
开始使用grep:
$ grep -oP "/player/\K.*" FILE
ABc12
ABC321
EGF987
egf751
Or shorter :
或更短:
$ grep -oP "[^/]/\K.*" FILE
ABc12
ABC321
EGF987
egf751
Or without -P(pcre) option :
或者没有-P(pcre) 选项:
$ grep -o '[^/]\+$' FILE
ABc12
ABC321
EGF987
egf751
Or with pure bash:
或者用纯bash:
$ IFS=/ oIFS=$IFS
$ while read a b c; do echo $c; done < FILE
ABc12
ABC321
EGF987
egf751
$ IFS=$oIFS
回答by Ignacio Vazquez-Abrams
Stop using grep.
停止使用 grep。
$ awk -F/ ' == "player" { print }' input.txt
ABc12
ABC321
EGF987
egf751
回答by Steve
One way using GNU grepand a positive lookbehind:
一种使用方法GNU grep和积极的回顾:
grep -oP '(?<=^/player/).*' file.txt
Results:
结果:
ABc12
ABC321
EGF987
egf751

