sed:当找到字符串时,从模式之间的文件中删除多行

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

我正在尝试删除两个模式之间的行 (Beginn: info / End: } ),其中匹配一个字符串。

这是我的文件:

# 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";
}

我的 sed 脚本

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

我的 sed 脚本可以正确处理字符串 0202030456789,并删除从信息 text02text {} 的所有行。 尝试使用 text01text 中的字符串 0102030456789,然后从“ xim “43ndf392rfhf3}” 行删除 sed};”和行

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

请勿删除。

如何删除所有行,在哪里找到字符串?

谢谢!

来自您的样本

试试这个:

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

但我更喜欢:

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

这可以写成(在下):

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

或者也许:

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

以防止在文件末尾丢失 }

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