简体   繁体   中英

sed: Delete multiple lines from files between pattern, when found string

I'm trying to delete lines between two pattern (Beginn: info / End: } ), where match a string.

Here my file:

# bla bla
# bla bla
# bla bla
# bla bla

nnssjnds nkjdnds "nsrnsnmks" ffsns {
  is on or off at 9:12:43 23/02/2015;
  is nass or trocken at 08:32:12 22/02/2015;
}

info text01text {
  beginn 30/04/2015 10:00:04;
  end    30/04/2015 19:00:04;
  check1 30/04/2015 11:30:04;
  check2 30/04/2015 13:00:04;
  check3 30/04/2015 16:00:04;
  Check4 30/04/2015 18:00:04;
  build top end;
  mix water 0102030456789;
  xim "43ndf392rfhf<DF>3}";
  test space = "ALLFINE";
  eman cpre "ann";
}
info text02text {
  beginn 30/04/2015 10:00:04;
  end    30/04/2015 19:00:04;
  check1 30/04/2015 11:30:04;
  check2 30/04/2015 13:00:04;
  check3 30/04/2015 16:00:04;
  Check4 30/04/2015 18:00:04;
  build top end;
  mix water 0202030456789;
  xim "43ndf392rfhf<DF>3";
  test space2 = "ALLFINE2";
  eman cpre2 "ann2";
}
info text03text {
  beginn 30/04/2015 10:00:04;
  end    30/04/2015 19:00:04;
  check1 30/04/2015 11:30:04;
  check2 30/04/2015 13:00:04;
  check3 30/04/2015 16:00:04;
  Check4 30/04/2015 18:00:04;
  build top end;
  mix water 0302030456789;
  xim "43ndf392rfhf<DF>3";
  test space3 = "ALLFINE3";
  eman cpre3 "ann3";
}

my sed script

:point
/^info/,/^}/ {
   /}/!{
      $!{
         N;
         bpoint
      }
   }
   /0202030456789/d;
}

My sed script worked correct with the string 0202030456789 and delete all lines from info text02text { to } . Try it with the string 0102030456789 from text01text , then delete sed to } from line " xim "43ndf392rfhf3}";" and the lines

test space = "ALLFINE";
eman cpre "ann";
}

don't delete.

How is it possible to delete all lines, where found the string?

Thanks!

From your sample

try this:

:point
/^info/,/^}/ {
   /\n *}/!{
      $!{
         N;
         bpoint
      }
   }
   /0202030456789/d;
}

But I prefer:

/^info .*{/ {
  :a;
    N;
    /\n *}/!ba;
    /0202030456789/d;
}

This could by written (under ):

mixToDel=0302030456789
sed < file '/^info .*{/{ :a;N;/\n *}/!ba;/'$mixToDel'/d;}'

Or maybe:

sed < file '/^info .*{/{ :a;N;$!{/\n *}/!ba};/'$mixToDel'/d;}'

to prevent missing } at end of file:

/^info .*{/{
  :a;
    N;
    $!{
        /\n *}/!ba
    };
    /'$mixToDel'/d;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM