第一次比赛后删除行 [英] Remove lines after first match

查看:60
本文介绍了第一次比赛后删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除包含单词"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

但是,我正在寻找一种更短且可移植的解决方案(此解决方案包含Wi​​ndows特定的命令"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比赛之前删除所有行

推荐答案

awk '{print} /fox/{exit}' file

使用GNU sed:

sed '0,/fox/!d' file

sed -n '0,/fox/p' file

这篇关于第一次比赛后删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆