簡體   English   中英

Sed:刪除所有出現的模式后的行

[英]Sed: delete lines after a pattern for all occurences

我需要sed的幫助。 我正在嘗試刪除文件中所有出現的模式后的3行。 我做

sed '/pattern/,+3d' file.

這只會刪除3行和第一次出現的模式,但只會刪除第二次出現的模式,但不會刪除之后的行,這確實令人困惑。 誰能幫我解決我的錯?

我認為awk更好地完成了任務。 例如,

$ cat file
1
2
4
a0
1
a1
1
2
3
4
5

awk '
flag   { i ++            }
i == 3 { flag = 0        }
!flag
/a/    { flag = 1; i = 0 }
' file

產量

1
2
4
a0
3
4
5

這可能對您有用(GNU sed):

sed -n '/regexp/{p;:a;n;//ba;n;//ba;n;//ba;d};p' file

如果遇到正則表達式,請打印當前行,然后刪除以下3行。 在讀取這3行的任何時候,都會發生正則表達式,請重置計數。

如果正則表達式也要刪除,請使用:

sed -n '/regexp/{:a;n;//ba;n;//ba;n;//ba;d};p' file

暫無
暫無

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

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