71

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'
Tyilo
  • 5,981
  • 7
    Must use sed? grep's -o switch looks like a shorter and cleaner way: echo "atestb" | grep -o 'test'. – manatwork Jul 16 '12 at 07:54
  • In case you are trying to output only the matching portion BETWEEN two known strings, try echo "aSomethingYouWantb" | sed -En 's/a(.*)b/\1/p' – luckman212 Oct 17 '20 at 00:59

1 Answers1

73

You need to match the whole line:

echo "atestb" | sed -n 's/.*\(test\).*/\1/p'

or

echo "atestb" | sed 's/.*\(test\).*/\1/'
Thor
  • 17,182