在linux shell脚本中如何找到1个字符串并删除前4行

-2 linux shell perl awk sed

我希望每次在我的文件中都有包含模式"黄色"的行,它会删除此行和前面的4行.

例如,我的文件:

orange
green
grey
black
blue
yellow
red
green
grey
white
black
yellow
blue
Run Code Online (Sandbox Code Playgroud)

输出文件将是:

orange
red
blue
Run Code Online (Sandbox Code Playgroud)

非常感谢.如果可能的话,使用unix标准命令,如awk,sed,cut,grep,perl脚本,php脚本,python脚本...(我的条目文件非常大,几乎是2Gb,2500万行)

anu*_*ava 5

使用tacawk:

tac file | awk '/yellow/{p=5} !p--' | tac
orange
red
blue
Run Code Online (Sandbox Code Playgroud)
  • tac - 反向连接和打印文件
  • awk 命令将计数器设置为5,并跳过下面4行的打印