#469159 option for fgrep/grep --file to print matching regexps

Package:
grep
Source:
grep
Description:
GNU grep, egrep and fgrep
Submitter:
martin f krafft
Date:
2014-11-20 08:57:12 UTC
Severity:
wishlist
#469159#3
Date:
2008-03-03 13:36:24 UTC
From:
To:
It would be cool if grep could get an option to print the regexp
which matched in place of the matching lines. E.g.

$ cat <<_eof > patterns
.o.
.a.
_eof
$ echo foo | egrep --file=patterns --print-matching-regexp
.o.
$ echo foo | egrep --file=patterns --print-matching-regexp-lineno
1
$ echo bar | egrep --file=patterns --print-matching-regexp
.a.
$ echo bar | egrep --file=patterns --print-matching-regexp-lineno
2
$ echo foo | egrep --file=patterns --file=patterns2 --print-matching-regexp
patterns:.o.
$ echo foo | egrep --file=patterns --file=patterns2 --print-matching-regexp-lineno
patterns:1
$ echo bar | egrep --file=patterns --file=patterns2 --print-matching-regexp
patterns:.a.
$ echo bar | egrep --file=patterns --file=patterns2 --print-matching-regexp-lineno
patterns:2

#469159#6
Date:
2008-03-03 16:05:40 UTC
From:
To:
Interesting idea.  OTOH it's yet another option for which grep |grep
doesn't mean "lines that match both REs".  Did you try --color?

#469159#9
Date:
2008-03-03 16:25:38 UTC
From:
To:
also sprach Justin Pryzby <justinpryzby@users.sourceforge.net> [2008.03.03.1705 +0100]:

Huh?

I use it all the time, but it's not useful to be used from scripts,
nor does it tell me which regexp actually matched.

#469159#14
Date:
2008-03-03 16:36:16 UTC
From:
To:
I divide the grep options into two categories, determined by whether you
can do pipe grep output into a 2nd grep and get precisely those lines
that match both REs.  grep -e a -e b and grep -Ee 'a\|b' exit to select
lines that match either.

grep -EFGPefiwxh allow that.  OTOH grep -ABCbnHoclL don't.