bashshellgrep

Grep - how to output only the content of a capturing group


I am trying to find a way for grep to output only the content of a capturing group. For instance, if I have the following file:

hello1, please match me
hello2, please do not match me

I would like

grep -Eo '(hello[0-9]+), please match me' file

To output hello1. However it outputs hello1, please match me.

Now, I know that grep -Po 'hello[0-9]+(?=, please match me)' will do the trick, but I'm thinking there must be a way to simply return a capturing group, but I couldn't find any info (on the net and in man grep).

Is it possible, or are capturing groups only meant to be backrefenced ? It would seem weird to me if there was no way of doing that.

Thank you for your time, and feel free to critique the way this post is constructed!


Solution

  • If you have either pcregrep or pcre2grep you can use the -o1 command-line flag to request that only capture group 1 is output. (Or change 1 to some other number if there are more captures in the regex.)

    You can use the -oN command more than once if you want to output more than one capture group.

    As far as I know, grep -P does not implement this extension. You'll find pcre2grep in Debian/Ubuntu package pcre2-utils. pcregrep is in package pcregrep.