Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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'
share|improve this question
    
Must use sed? grep's -o switch looks like a shorter and cleaner way: echo "atestb" | grep -o 'test'. –  manatwork Jul 16 '12 at 7:54
    
I can't use sed because the real thing is like echo 'afoo="test"b' | sed -n 's/.*foo="\([^"]*\)".*/\1/p', where I only want to match test. –  Tyilo Jul 16 '12 at 15:53
add comment

marked as duplicate by Gilles, Mat, Renan, jasonwryan, Stéphane Gimenez Aug 20 '12 at 17:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 6 down vote accepted

You need to match the whole line:

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

or

echo "atestb" | sed 's/.*\(test\).*/\1/'
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.