Awk grep-v多行同时传输

Awk grep-v多行同时传输,awk,grep,Awk,Grep,我想过滤包含“pattern”的行,并过滤以下5行 类似于grep-v-a5'pattern'myfile.txt的东西,输出: other other other other other other 我对linux shell解决方案、grep、awk、sed感兴趣。。。 Thx myfile.txt: other other other pattern follow1 follow2 follow3 follow4 follow5 other other other pattern fol

我想过滤包含“pattern”的行,并过滤以下5行

类似于
grep-v-a5'pattern'myfile.txt的东西,输出:

other
other
other
other
other
other
我对linux shell解决方案、grep、awk、sed感兴趣。。。 Thx

myfile.txt:

other
other
other
pattern
follow1
follow2
follow3
follow4
follow5
other
other
other
pattern
follow1
follow2
follow3
follow4
follow5
other
other
other
other
other
other

您可以使用
awk

awk '/pattern/{c=5;next} !(c&&c--)' file
基本上:我们正在减少每一行输入的整数
c
。当
c
0
时,我们正在打印行*(见下文)注意:
c
将在首次使用时由awk自动初始化为
0


当找到单词
模式
时,我们将
c
设置为
5
,这使得
c--您可以使用
awk

awk '/pattern/{c=5;next} !(c&&c--)' file
基本上:我们正在减少每一行输入的整数
c
。当
c
0
时,我们正在打印行*(见下文)注意:
c
将在首次使用时由awk自动初始化为
0

当找到单词
模式
时,我们将
c
设置为
5
,这使得
c--我使用以下方法:

head -$(wc -l myfile.txt | awk '{print $1-5 }') myfile.txt | grep -v "whatever"
这意味着:

wc -l myfile.txt    : how many lines (but it also shows the filename)
awk '{print $1}'    : only show the amount of lines
awk '{print $1-5 }' : we don't want the last five lines
head ...            : show the first ... lines (which means, leave out the last five)
grep -v "..."       : this part you know :-)
我是这样做的:

head -$(wc -l myfile.txt | awk '{print $1-5 }') myfile.txt | grep -v "whatever"
这意味着:

wc -l myfile.txt    : how many lines (but it also shows the filename)
awk '{print $1}'    : only show the amount of lines
awk '{print $1-5 }' : we don't want the last five lines
head ...            : show the first ... lines (which means, leave out the last five)
grep -v "..."       : this part you know :-)
使用
GNU sed
(应该可以,因为OP提到了
Linux

这将删除与给定正则表达式匹配的行和
GNU-sed
后面的5行(应该可以,因为OP提到了
Linux


这将删除与给定正则表达式匹配的行和后面的5行

这将给我“坏”部分:“模式follow1 follow2 follow3 follow4 follow5”,我需要“其他,其他,其他”是。我需要有“其他”内容的行。请看,选择你需要的一行,并根据需要进行按摩(提示-否定
g
)。这给了我“不好”的部分:“模式跟随1跟随2跟随3跟随4跟随5”,我需要“其他,其他,其他”是的。我需要包含“其他”内容的行。请看,选择您需要的一行,并根据需要进行按摩(提示-否定
g
)。将其更改为
!(c&&c--)
我将删除我的答案。对于大型文件
c--
将是一个problem@oguzismail你应该保留你的。谢谢你的提示!!如果这不是我第一百万次问这个问题的话,我会的,但是不,谢谢你,请把
模式
改成
regexp
。我们不是在编织毛衣:-)。我知道你对此很挑剔,我通常使用
regexp
来表示正则表达式。在这种情况下,OP只是在问题中使用了单词
模式
,并保留它使我的示例可以在给定的输入下执行。将其更改为
!(c&&c--)
我将删除我的答案。对于大型文件
c--
将是一个problem@oguzismail你应该保留你的。谢谢你的提示!!如果这不是我第一百万次问这个问题的话,我会的,但是不,谢谢你,请把
模式
改成
regexp
。我们不是在编织毛衣:-)。我知道你对此很挑剔,我通常使用
regexp
来表示正则表达式。在本例中,OP只是在问题中使用了单词
pattern
,并保持它使我的示例可以使用给定的输入执行。