简体   繁体   English

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

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

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 line and The fourth line in addition to the string. 我想删除文件中的字符串及其后的所有行,因此它将删除字符串之外The third lineThe fourth line The file would become: 该文件将变为:

The first line

I've searched for a solution on google, and it seems that I should use sed . 我在google上搜索了一种解决方案,看来我应该使用sed Something like: 就像是:

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

This says "when you reach the line that matches the pattern quit, otherwise print each line". 这表示“当您到达与模式退出匹配的行时,否则打印每行”。 The -n option prevents implicit printing and the p command 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 -n option (no implicit print) is not used). 这表示“打印所有行并在到达匹配的行时退出”(不使用-n选项(不使用隐式打印))。

See man sed for additional information. 有关其他信息,请参见man sed

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
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"

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

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

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
awk '/The second line/{exit}1' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用另一个文件中的内容替换一个文件中第XX行之后的所有行? - How do I replace all lines after line XX in one file with content from another file? 在文本文件中,如何删除紧随其后的行的所有行子集? - In a text file, how do I delete all lines subset of their immediately following line? 如何从文本文件中的第一个空行开始删除所有行? - How to remove all lines from a text file starting at first empty line? 如何删除包含匹配单词的文件中给定间隔的行? - How do I delete lines of a given interval in a file containing a matching word? 阅读后如何从文件中删除行? - How to delete lines from file after reading it? 如何使用Tcl / Expect从文件中删除与模式匹配的行 - How to delete lines matching a pattern from a file using Tcl/Expect 脚本删除一个单词以外的所有/ n行数(最后一行除外) - Script to delete all /n number of lines starting from a word except last line 如何删除文件中以某些数字开头的行? - How to delete lines starting with certain numbers in a file? Linux:从第二次出现开始的具有匹配字符串的XML文件中删除行的命令 - Linux: Command to delete line(s) from XML file with matching string starting with the 2nd occurrence 如何从每个具有匹配文本的文件中删除行? - How do I remove lines from every file that has a matching text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM