[英]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 line
和The 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
选项(不使用隐式打印))。
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.