print string between two characters or strings

From thelinuxwiki
Revision as of 19:04, 21 August 2013 by Nighthawk (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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)"|awk -F'[(|)]' '{print $2}'
foo
$ echo "bla=@@foo@@"|awk -F'[@@|@@]' '{print $3}'
foo

with sed:

$ echo "blah(foo)"|sed -n 's/.*(\([^ ]*\))/\1/p'
foo
echo "aaafoobbb"|sed -n 's/.*aaa\([^ ]*\)bbb/\1/p'
foo