Possible Duplicate:
Extracting a regex matched with 'sed' without printing the surrounding characters
How do I make this only print test
:
echo "atestb" | sed -n 's/\(test\)/\1/p'
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityPossible Duplicate:
Extracting a regex matched with 'sed' without printing the surrounding characters
How do I make this only print test
:
echo "atestb" | sed -n 's/\(test\)/\1/p'
You need to match the whole line:
echo "atestb" | sed -n 's/.*\(test\).*/\1/p'
or
echo "atestb" | sed 's/.*\(test\).*/\1/'
-r
or ` --regexp-extended` option otherwise I was getting invalid reference \1 on
s' command's RHS ` error.
– Daniel Sokolowski
Aug 11 '14 at 16:12
-r
to the above you should get an error saying "invalid reference". What version of sed are you using?
– Thor
Aug 11 '14 at 20:46
sed
?grep
's-o
switch looks like a shorter and cleaner way:echo "atestb" | grep -o 'test'
. – manatwork Jul 16 '12 at 7:54echo "aSomethingYouWantb" | sed -En 's/a(.*)b/\1/p'
– luckman212 Oct 17 '20 at 0:59