Say I have a file:
# file: 'test.txt'
foobar bash 1
bash
foobar happy
foobar
I only want to know what words appear after "foobar", so I can use this regex:
"foobar \(\w\+\)"
The parenthesis indicate that I have a special interest in the word right after foobar. But when I do a grep "foobar \(\w\+\)" test.txt
, I get the entire lines that match the entire regex, rather than just "the word after foobar":
foobar bash 1
foobar happy
I would much prefer that the output of that command looked like this:
bash
happy
Is there a way to tell grep to only output the items that match the grouping (or a specific grouping) in a regular expression?
perl -lne 'print $1 if /foobar (\w+)/' < test.txt
– vault Dec 19 '16 at 11:56+
? – Sébastien Jan 29 '20 at 09:14(
,)
, and+
. – Cory Klein Jan 29 '20 at 15:54\(
,\)
,\+
in GRE. Use ERE = egrep, when you like(
,)
,+
– Hrobky Oct 14 '22 at 15:44