如何删除从匹配的行后启动文件中的所有行? [英] How do I delete all lines in a file starting from after a matching line?

查看:85
本文介绍了如何删除从匹配的行后启动文件中的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这是由几行文本文件:

I have a file which is made up of several lines of text:

The first line
The second line
The third line
The fourth line

我有一个字符串这是行之一:第二行

我要删除字符串,并在文件中后,所有行,所以它会删除第三行第四行除了字符串。该文件将成为:

I want to delete the string and all lines after it in the file, so it will delete The third line and The fourth line in addition to the string. The file would become:

The first line

我搜索对谷歌的解决方案,它似乎是我应该使用 SED 。是这样的:

sed 'linenum,$d' file

但如何找到字符串的行号?或者说,怎么回事,应该怎么办呢?

But how to find the line number of the string? Or, how else should I do it?

推荐答案

如果您不希望打印的匹配行(或任何以下行):

If you don't want to print the matched line (or any following lines):

sed -n '/The second line/q;p' inputfile

sed '/The second line/,$d' inputfile

但第一个比较快。

but the first one is faster.

如果你想打印匹配行,但不是任何以下行:

If you do want to print the matched line, but not any following lines:

sed '/The second line/q' inputfile

这篇关于如何删除从匹配的行后启动文件中的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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