Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When running the following

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

The output is:


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:


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

share|improve this question

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
You can also cheat with awk like this: echo "abc>asf<tfg"|awk -F'<|>' '{print $2}' – Yaron Nov 15 '15 at 10:26

Your Answer


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.