Difference between revisions of "print string between two characters or strings"

From thelinuxwiki
Jump to: navigation, search
(Created page with "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 "b...")
 
Line 5: Line 5:
 
  $ echo "bla(foo)"|awk -F'[(|)]' '{print $2}'
 
  $ echo "bla(foo)"|awk -F'[(|)]' '{print $2}'
 
  foo
 
  foo
 +
 
  $ echo "bla=@@foo@@"|awk -F'[@@|@@]' '{print $3}'
 
  $ echo "bla=@@foo@@"|awk -F'[@@|@@]' '{print $3}'
 
  foo
 
  foo
Line 12: Line 13:
 
  $ echo "blah(foo)"|sed -n 's/.*(\([^ ]*\))/\1/p'
 
  $ echo "blah(foo)"|sed -n 's/.*(\([^ ]*\))/\1/p'
 
  foo
 
  foo
  echo "aaafoobbb"|sed -n 's/.*aaa\([^ ]*\)bbb/\1/p'
+
 
 +
  $echo "aaafoobbb"|sed -n 's/.*aaa\([^ ]*\)bbb/\1/p'
 
  foo
 
  foo
 +
 
[[category:awk]]
 
[[category:awk]]
 
[[category:sed]]
 
[[category:sed]]

Revision as of 19:04, 21 August 2013

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