Linux perl one-liner 像grep?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4794145/
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-08-05 02:37:22  来源:igfitidea点击:

perl one-liner like grep?

linuxperlgrep

提问by barlop

i'd like perl to do a one-liner like grep

我想让 perl 做一个像 grep

a bit like this, but i'm not sure what to add to make it work

有点像这样,但我不确定要添加什么才能使其工作

$ (echo a ; echo b ; echo c) | perl -e 'a'

ADDED My answer here covers that and more
https://superuser.com/questions/416419/perl-for-matching-with-regex-in-terminal

添加我在这里的回答涵盖了这一点以及更多
https://superuser.com/questions/416419/perl-for-matching-with-regex-in-terminal

采纳答案by Eugene Yarmash

(echo a; echo b; echo c) | perl -ne 'print if /a/'

回答by kurumi

You can do the same with Ruby, if you can afford other options

如果你能负担得起其他选择,你可以用 Ruby 做同样的事情

$ (echo a; echo b; echo c) | ruby -ne 'print if /a/'
a
$ (echo a; echo b; echo c) | ruby -ne 'print if $_["a"]'
a

回答by Joel Berger

To echo mob's comment:

回应暴徒的评论:

If you want to use Perl regexes try ack: http://betterthangrep.com/

如果您想使用 Perl 正则表达式,请尝试ackhttp: //betterthangrep.com/