Grep regex matches, groups

Hello,

I am searching all over the place for this, just not finding anything solid :frowning:

I want to do be able to access the groups that are matched with grep (either with extended regex, or perl compatible regex). For instance:

echo "abcd" | egrep "a(b(c(d)))"

Of course this returns "abcd", but I would like to have access to each regex group that matched as well. In just bash, I believe there is a SUBMATCH array, but it seems to not be set in this case.

Any help would be greatly appreciated! Thanks!!

I think you have to use perl or awk:

perl -ne '$,=" ... "; chomp; /a(b(c(d)))/ && print $_,$&,$1,$2,$3,"\n";'

The output in order is : the line that matched, the text that matched the entire regexp, the first (outermost) group, the second, group, the third group, (newline).