简体   繁体   中英

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.

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

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