简体   繁体   English

如何使用 sed 删除从 pattern_1 到 pattern_2 第二次出现的行?

[英]How to delete lines from pattern_1 to the second occurence of pattern_2 with sed?

i need to find a command to delete lines from pattern_1 to the second occurence of pattern_2 (pattern_1 and second occurence of pattern_2 included) with sed.我需要使用 sed 找到一个命令来删除从 pattern_1 到 pattern_2 的第二次出现(包括 pattern_1 和 pattern_2 的第二次出现)的行。

random_line_1
pattern_1
pattern_2
random_line_3
random_line_4
random_line_5
pattern_2
random_line_6

i need to obtain:我需要获得:

random_line_1
random_line_6

I tried lots of commands inspired by what i have found everywhere but nothing works...我尝试了很多命令,灵感来自我在各处发现的东西,但没有任何效果......

any idea?任何想法?

Would you please try the following:请您尝试以下方法:

sed -n '
/pattern_1/ {                   ;# if the line matches "pattern_1"
    :l1                         ;# then enter a loop for "pattern_2"
    n
    /pattern_2/ {               ;# if the line matches the 1st "pattern_2"
        :l2                     ;# then enter an inner loop for next "pattern_2"
        n
        /pattern_2/ {           ;# if the line matches the 2nd "pattern_2"
            b                   ;# then exit the loop
        }
        b l2                    ;# else jump to "l2"
    }
    b l1
}
p                               ;# print the pattern space
' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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