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...")
 
 
(2 intermediate revisions by one user not shown)
Line 3: Line 3:
 
print the text between two tags/characters/strings.
 
print the text between two tags/characters/strings.
  
  $ echo "bla(foo)"|awk -F'[(|)]' '{print $2}'
+
  $ echo "bla(foo)ar"|awk -F'[(|)]' '{print $2}'
 
  foo
 
  foo
 +
 
  $ echo "bla=@@foo@@"|awk -F'[@@|@@]' '{print $3}'
 
  $ echo "bla=@@foo@@"|awk -F'[@@|@@]' '{print $3}'
 
  foo
 
  foo
Line 10: Line 11:
 
with sed:
 
with sed:
  
  $ echo "blah(foo)"|sed -n 's/.*(\([^ ]*\))/\1/p'
+
  $ echo "blah(foo)ar"|sed -n 's/.*(\([^ ]*\))/\1/p' <<< not working!!!
  foo
+
  fooar
  echo "aaafoobbb"|sed -n 's/.*aaa\([^ ]*\)bbb/\1/p'
+
 
  foo
+
  $ echo "aaafoobbbar"|sed -n 's/.*aaa\([^ ]*\)bbb/\1/p'   <<< not working!!!
 +
  fooar
 +
 
 
[[category:awk]]
 
[[category:awk]]
 
[[category:sed]]
 
[[category:sed]]

Latest revision as of 17:50, 4 December 2013

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