This question already has answers here:
Delete only fully formed line ranges from a text file while ignoring those that only have a start delimiter
                                
                                    (4个答案)
                                
                        
                                3年前关闭。
            
                    
我有一个像这样的文件:

Sample text1 <TAG> some text
Line1
Line2
<INSERT>
Sample text3 <TAG> some text
Line3
<DELETE>
Sample text4 <TAG> some text
Line4
Line5
<INSERT>
Sample text5 <TAG> some text
Line6
<DELETE>


我想删除DELETE之前和TAG之前的行,以便它看起来像这样:

Sample text1 <TAG> some text
Line1
Line2
<INSERT>
Sample text4 <TAG> some text
Line4
Line5
<INSERT>

最佳答案

在awk中使用我在注释中提到的先前解决方案:

$ cat foo.awk
/TAG/ { printf "%s", b; b="" }                       # at START output buffer and empty it
{ b=b $0 ORS }                                       # gather buffer
/DELETE/ { b="" }                                    # at empty buffer at END also


运行:

$ awk -f foo.awk foo2
Sample text1 <TAG> some text
Line1
Line2
<INSERT>
Sample text4 <TAG> some text
Line4
Line5
<INSERT>

关于linux - 如何删除关键字之前及其匹配字符串标记之前的行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40300548/

10-14 06:44