[英]How to delete lines from pattern_1 to the second occurence of pattern_2 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
我需要獲得:
random_line_1
random_line_6
我嘗試了很多命令,靈感來自我在各處發現的東西,但沒有任何效果......
任何想法?
請您嘗試以下方法:
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.