Linux 如何从匹配行之后删除文件中的所有行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5227295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:27:47  来源:igfitidea点击:

How do I delete all lines in a file starting from after a matching line?

linuxbashsed

提问by DocWiki

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 have a string which is one of the lines: The second line

我有一个字符串,它是其中一行: The second line

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

我想删除文件中的字符串及其后的所有行,因此它将删除The third lineThe fourth line除字符串之外。该文件将变为:

The first line

I've searched for a solution on google, and it seems that I should use sed. Something like:

我在谷歌上搜索了一个解决方案,似乎我应该使用sed. 就像是:

sed 'linenum,$d' file

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

但是如何找到字符串的行号呢?或者,我还应该怎么做?

采纳答案by Paused until further notice.

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

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

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

This says "when you reach the line that matches the pattern quit, otherwise print each line". The -noption prevents implicit printing and the pcommand is required to explicitly print lines.

这表示“当您到达与模式匹配的行时退出,否则打印每一行”。该-n选项可防止隐式打印,并且p需要该命令来显式打印行。

or

或者

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

This says "delete all lines from the output starting at the matched line and continuing to the end of the file".

这表示“从匹配行开始并继续到文件末尾的输出中删除所有行”。

but the first one is faster. However it will quit processing completely so if you have multiple files as arguments, the ones after the first matching file won't be processed. In this case, the delete form is better.

但第一个更快。但是它将完全退出处理,因此如果您有多个文件作为参数,则不会处理第一个匹配文件之后的文件。在这种情况下,删除形式更好。

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

如果您确实想打印匹配的行,但不打印以下任何行:

sed '/The second line/q' inputfile

This says "print all lines and quit when the matched line is reached" (the -noption (no implicit print) is not used).

这表示“打印所有行并在到达匹配行时退出”(-n不使用选项(无隐式打印))。

See man sedfor additional information.

有关其他信息,请参阅man sed

回答by Erik

sed '/The second line/q0' file

Or, without gnu sed:

或者,没有 gnu sed:

sed '/The second line/q' file

Or, using grep:

或者,使用 grep:

grep -B 9999999 "The second line"

回答by sush

First add the line number and delete the line

先添加行号,删除行

cat new.txt 
The first line
The second line
The third line
The fourth line

 cat new.txt  | nl
     1  The first line
     2  The second line
     3  The third line
     4  The fourth line



cat new.txt  | nl | sed  "/2/d"
     1  The first line
     3  The third line
     4  The fourth line

cat new.txt  |  nl |sed  "3d;4d"
     1  The first line
     2  The second line

using awk

使用 awk

awk 'NR!=3 && NR!=4' new.txt 
The first line
The second line

回答by Joel Arnold

Using awk (not showing the matched line)

使用 awk(不显示匹配的行)

awk '/pattern/ {exit} {print}' file.txt

回答by kurumi

awk '/The second line/{exit}1' file

回答by jaap

This is a bit shorter than other given solutions. Quit using capital Q avoids printing the current line.

这比其他给定的解决方案要短一些。退出使用大写 Q 避免打印当前行。

 sed '/The second line/Q' file

To actually delete the lines you can use the same syntax.

要实际删除行,您可以使用相同的语法。

 sed -i '/The second line/Q' file