我需要删除包含单词"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"
类似的问题:
-
如何删除模式最后一次出现后的所有行吗?
如何在保留比赛之前删除行它吗?
在与sed比赛之前删除所有行
如何使用sed在遇到模式[[ERROR] -17-12-2015]之前从第一行开始删除行,直到行之间?
如何删除字符串的第一次出现和最后一次出现之后的所有行?
推荐答案awk '{print} /fox/{exit}' file
使用GNU sed:
sed '0,/fox/!d' file或
sed -n '0,/fox/p' file更多推荐
第一次比赛后删除行
发布评论