从命令行打印正则表达式的所有匹配项?

发布于 2024-11-13 23:55:00 字数 284 浏览 1 评论 0原文

在 unix 命令行上将所有匹配项(每个匹配项一行或每行输入一行)打印到正则表达式的最简单方法是什么?请注意,每行输入可能有 0 个或超过 1 个匹配项。

我认为必须有某种方法可以使用 sed、awk、grep 和/或 perl 来完成此操作,并且我希望有一个简单的命令行解决方案,以便将来需要时它会显示在我的 bash 历史记录中。

编辑:为了澄清,我不想打印所有匹配的行,只想打印正则表达式的匹配项。例如,一行可能有 1000 个字符,但只有两个 10 个字符与正则表达式匹配。我只对那两个 10 个字符的比赛感兴趣。

What's the simplest way to print all matches (either one line per match or one line per line of input) to a regular expression on a unix command line? Note that there may be 0 or more than 1 match per line of input.

I assume there must be some way to do this with sed, awk, grep, and/or perl, and I'm hoping for a simple command line solution so it will show up in my bash history when needed in the future.

EDIT: To clarify, I do not want to print all matching lines, only the matches to the regular expression. For example, a line might have 1000 characters, but there are only two 10-character matches to the regular expression. I'm only interested in those two 10-character matches.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眉目亦如画i 2024-11-20 23:55:00

假设您仅使用非捕获括号,

perl -wnE'say /yourregex/g'

perl -wnE'say for /yourregex/g'

示例使用:

$ echo -ne 'fod,food,fad\nbar\nfooooood\n' | perl -wnE'say for /fo*d/g'
fod
food
fooooood
$ echo -ne 'fod,food,fad\nbar\nfooooood\n' | perl -wnE'say /fo*d/g'
fodfood

fooooood

Assuming you only use non-capturing parentheses,

perl -wnE'say /yourregex/g'

or

perl -wnE'say for /yourregex/g'

Sample use:

$ echo -ne 'fod,food,fad\nbar\nfooooood\n' | perl -wnE'say for /fo*d/g'
fod
food
fooooood
$ echo -ne 'fod,food,fad\nbar\nfooooood\n' | perl -wnE'say /fo*d/g'
fodfood

fooooood
苦笑流年记忆 2024-11-20 23:55:00

除非我误解了你的问题,否则以下内容将解决问题

grep -o 'fo.*d' input.txt

有关更多详细信息,请参阅:

Unless I misunderstand your question, the following will do the trick

grep -o 'fo.*d' input.txt

For more details see:

你在我安 2024-11-20 23:55:00

离开注释,假设您从管道或其他方式在 STDIN 上传递了输入:

perl -e 'my $re=shift;$re=~qr{$re};while(<STDIN>){if(/($re)/g){print"$1\n"}while(m/\G.*?($re)/g){print"$1\n"}}'

用法:

cat SOME_TEXT_FILE | perl -e 'my $re=shift;$re=~qr{$re};while(<STDIN>){if(/($re)/g){print"$1\n"}while(m/\G.*?($re)/g){print"$1\n"}}' 'YOUR_REGEX'

或者我只是将整个乱七八糟的东西塞进 bash 函数...

bggrep ()
{
    if [ "x$1" != "x" ]; then
        perl -e 'my $re=shift;$re=~qr{$re};while(<STDIN>){if(/($re)/g){print"$1\n"}while(m/\G.*?($re)/g){print"$1\n"}}' $1;
    else
        echo "Usage: bggrep <regex>";
    fi
}

用法是相同的,只是看起来更干净:(

cat SOME_TEXT_FILE | bggrep 'YOUR_REGEX'

或者只需键入命令本身并输入文本以逐行匹配,但这似乎不是一个可能的用例:)。

示例(来自您的评论):

bash$ cat garbage
fod,food,fad
bar
fooooooood
bash$ cat garbage | perl -e 'my $re=shift;$re=~qr{$re};while(<STDIN>){if(/($re)/g){print"$1\n"}while(m/\G.*?($re)/g){print"$1\n"}}' 'fo*d'
fod
food
fooooooood

或...

bash$ cat garbage | bggrep 'fo*d'
fod
food
fooooooood

Going off the comment, and assuming you're passed the input from a pipe or otherwise on STDIN:

perl -e 'my $re=shift;$re=~qr{$re};while(<STDIN>){if(/($re)/g){print"$1\n"}while(m/\G.*?($re)/g){print"$1\n"}}'

Usage:

cat SOME_TEXT_FILE | perl -e 'my $re=shift;$re=~qr{$re};while(<STDIN>){if(/($re)/g){print"$1\n"}while(m/\G.*?($re)/g){print"$1\n"}}' 'YOUR_REGEX'

or I would just stuff that whole mess into a bash function...

bggrep ()
{
    if [ "x$1" != "x" ]; then
        perl -e 'my $re=shift;$re=~qr{$re};while(<STDIN>){if(/($re)/g){print"$1\n"}while(m/\G.*?($re)/g){print"$1\n"}}' $1;
    else
        echo "Usage: bggrep <regex>";
    fi
}

Usage is the same, just cleaner-looking:

cat SOME_TEXT_FILE | bggrep 'YOUR_REGEX'

(or just type the command itself and enter the text to match line-by-line, but that didn't seem a likely use case :).

Example (from your comment):

bash$ cat garbage
fod,food,fad
bar
fooooooood
bash$ cat garbage | perl -e 'my $re=shift;$re=~qr{$re};while(<STDIN>){if(/($re)/g){print"$1\n"}while(m/\G.*?($re)/g){print"$1\n"}}' 'fo*d'
fod
food
fooooooood

or...

bash$ cat garbage | bggrep 'fo*d'
fod
food
fooooooood
飞烟轻若梦 2024-11-20 23:55:00
perl -MSmart::Comments -ne '@a=m/(...)/g;print;' -e '### @a'
perl -MSmart::Comments -ne '@a=m/(...)/g;print;' -e '### @a'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文