print string between two characters or strings

From thelinuxwiki
Jump to: navigation, search

Howto print text between tags or characters with awk or sed

print the text between two tags/characters/strings.

$ echo "bla(foo)ar"|awk -F'[(|)]' '{print $2}'
foo
$ echo "bla=@@foo@@"|awk -F'[@@|@@]' '{print $3}'
foo

with sed:

$ echo "blah(foo)ar"|sed -n 's/.*(\([^ ]*\))/\1/p'  <<< not working!!!
fooar
$ echo "aaafoobbbar"|sed -n 's/.*aaa\([^ ]*\)bbb/\1/p'   <<< not working!!!
fooar