簡體   English   中英

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正確工作,並刪除info text02text { to }所有行。 嘗試使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM