[英]Perl oneliner in bash: print matches from complex regexp
I have this complex regex 我有这个复杂的正则表达式
/"_outV":([0-9]+),"_inV":([0-9]+),"_label":"([a-z\/]+)",/
and I need to parse a file (which is all on one single line) and output only the matched groups like 我需要解析一个文件(全部在一行上),然后仅输出匹配的组,例如
print $1 $2 $3
Currently the only almost working onliner is 目前只差一点工作 onliner是
perl -pe 'while(m/"_outV":([0-9]+)\,"_inV":([0-9]+)\,"_label":"([a-z\/]+)\"\,/g){print "$1 $2 $3\n";}'
But it ends up echoing also the entire file at the end, after the matches. 但是最终在匹配之后,它还会在最后回显整个文件。 How do I fix this? 我该如何解决?
I though that removing the -p
option would make the trick, but it doesn't. 我虽然删除了-p
选项可以解决问题,但事实并非如此。
Looks good to me. 对我来说看上去很好。 You need to replace the -p
with -n
and here is why . 您需要将-p
替换为-n
, 这就是原因 。
A few finer points: 一些要点:
,
and "
. 无需反斜杠那些,
和"
。 [0-9]
with \\d
. 您可以方便地将[0-9]
替换为\\d
。 /
. 通过为正则表达式使用其他定界符,您将不需要转义/
。 End result optimized 最终结果优化
perl -ne 'print "$1 $2 $3\n" while m{"_outV":(\d+),"_inV":(\d+),"_label":"([a-z/]+)",}g'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.