小编Wil*_*iam的帖子

删除与字符串的前 7 个匹配项对应的行(在模式范围内)

我需要从 txt 文件中的模式范围 (string1-string2) 中删除与字符串前 7 次出现相对应的行。

txt文件内容示例:

whatever
xpto string1 foo2
whatever1 
string2
xpto1 another_foo
xpto string2

string2 foo1

whatever
string2 another_xpto
string2 string2
foo xpto string2 whatever 
anything else foo string2
xpto
string2
foo whatever
Run Code Online (Sandbox Code Playgroud)

我需要一个带有 sed 范围的解决方案,类似这样:

sed '/string1/,/string2/d' file.txt

关键是我不知道如何延伸/string2/到与 string2 的第七个匹配相对应的行。理想的输出应该是:

whatever
anything else foo string2
xpto
string2
foo whatever
Run Code Online (Sandbox Code Playgroud)

sed awk text-processing

3
推荐指数
1
解决办法
187
查看次数

标签 统计

awk ×1

sed ×1

text-processing ×1