Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When running the following

echo "abc>asf<tfg"|sed 's/.*>\(.*\)</\1/'

The output is:

asftfg

I don't understand why tfg gets printed? My expected result is asf. Though, it works fine in the following.

expr `echo "abc>asf<tfg"`  : '.*>\(.*\)<'

Which gets me:

asf

And help me to extract the string between > and < when input is being received from a pipe.

share|improve this question
add comment

1 Answer

The s command in sed is a match-and-replace. In your case, .*>\(.*\)< is the pattern to be matched, and \1 is the template to replace it with.

When you try to find a match for .*>\(.*\)< at the beginning of abc>asf<tfg, the result is abc>asf<. This part of the string is then replaced with asf. tfg was not matched, so nothing happens to it.

Using .*>\(.*\)<.* as your match pattern should get you the results you want.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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