I am trying to find a way for grep to output only the content of a capturing group. If I have this 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
.
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.
Is it possible, or are capturing groups only meant to be backrefenced?
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
.