0

Is egrep able to output the results of parenthesized subexpressions?

I’m using macOS, and when I checked the man page for grep, it mentioned re_format(7). Checking man 7 re_format, I see that it does have support for parenthesized subexpressions and records them in the pmatch array.

However, it isn’t clear how to make egrep output just the parenthesized subexpressions in groups somehow.

Edward Ocampo-Gooding
  • 2,782
  • 1
  • 23
  • 29
  • some `grep` versions have `-o` option to output only matching portion of regex.. but that doesn't require capture groups... you'll have to add sample input lines and expected output... also, is grep the only command you can use? why not use sed/awk/perl etc – Sundeep May 07 '17 at 15:33
  • Greetings @Sundeep – `grep` is not the only option but I was curious if it could be done. Looks like I am indeed going to have to use a more specialized language like what you’re referencing. – Edward Ocampo-Gooding May 07 '17 at 20:19
  • Yes. Or no. It all depends what you mean. [edit] your question to show a [mcve] including concise, testable sample input and expected output to clarify what it is you want to do. – Ed Morton May 08 '17 at 07:17

2 Answers2

0

Is this what youre trying to do (using GNU awk for the 3rd arg to match()):

$ cat file
aa(bb(cc(dd)(ee)ff)(gg))hh

$ awk '{ while( match($0,/(.*)\(([^)]*)\)(.*)/,a) ) { print ++c, a[2]; $0=a[1] a[3]} print "rest:", $0 }' file
1 gg
2 ee
3 dd
4 ccff
5 bb
rest: aahh
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • This is cool and I appreciate your efforts, but I just wanted to know if it was possible with `egrep`. Thanks anyway! – Edward Ocampo-Gooding Jun 13 '17 at 18:42
  • That depends what `it` is. See my comment under your question. – Ed Morton Jun 13 '17 at 18:44
  • I’m stuck for time at the moment (thanks for your continued efforts!) but if you search for “parenthesized subexpression” in https://www.freebsd.org/cgi/man.cgi?query=re_format&sektion=7 then you can see what I’m talking about. I want `grep` to just output those matches. – Edward Ocampo-Gooding Jun 13 '17 at 18:50
  • You're welcome but you'd be amazed how much less time I have to work on your problem than you do :-). So no I won't be searching for anything or trying to figure out your issue otherwise. See [ask] then do that... – Ed Morton Jun 13 '17 at 18:52
  • 1
    Haha I believe you Have a good one and thanks for the kind effort earlier! – Edward Ocampo-Gooding Jun 14 '17 at 02:06
0

No, egrep is not able to output the results of parenthesized subexpressions.

Edward Ocampo-Gooding
  • 2,782
  • 1
  • 23
  • 29