开发者

Print all matches of a regular expression from the command line?

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 t开发者_如何学Co 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.


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


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

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

For more details see:

  • GNU grep (most platforms)
  • Solaris grep
  • AIX grep
  • HP-UX grep


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


perl -MSmart::Comments -ne '@a=m/(...)/g;print;' -e '### @a'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜