第一次比赛后删除行 [英] Remove lines after first match
问题描述
我需要删除包含单词"fox"的第一行之后的所有行
I need to remove all lines after the first line that contains the word "fox"
例如,以下输入文件"in.txt"
Say for example for the following input file "in.txt"
The quick
brown fox jumps
over
the
lazy dog
the second fox
is hunting
结果将是
The quick
brown fox jumps
我更喜欢用awk或sed编写的脚本,但是其他任何命令行工具都很好,例如perl或php或python等.
I prefer a script made in awk or sed but any other command line tools are good, like perl or php or python etc.
出于搜索目的,我将添加以下行:
For searching purposes I will add this line:
出现后删除行
以后
我在Windows中使用gnuwin32工具,而我能找到的解决方案是:
I am using gnuwin32 tools in Windows, and the solution I could find was this one:
grep -m1 -n fox in.txt | cut -f1 -d":" > var.txt
set /p MyNumber=<var.txt
head -%MyNumber% in.txt > out.txt
但是,我正在寻找一种更短且可移植的解决方案(此解决方案包含Windows特定的命令"set/p"
However, I am looking for a solution that is shorter and that is portable (this one contains Windows specific command "set /p"
类似的问题:
推荐答案
使用GNU sed: 或 这篇关于第一次比赛后删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!awk '{print} /fox/{exit}' file
sed '0,/fox/!d' file
sed -n '0,/fox/p' file