在包含特定字符串的文本文件中删除行。

时间:2022-06-03 02:22:58

How would I use sed to delete all lines in a text file that contain a specific string?

如何使用sed来删除包含特定字符串的文本文件中的所有行?

14 个解决方案

#1


1895  

To remove the line and print the output to standard out:

将输出移去并打印到标准输出:

sed '/pattern to match/d' ./infile

To directly modify the file:

直接修改文件:

sed -i '/pattern to match/d' ./infile

To directly modify the file (and create a backup):

直接修改文件(并创建备份):

sed -i.bak '/pattern to match/d' ./infile

For Mac OS X users:

Mac OS X用户:

sed -i '' '/pattern/d' ./infile

#2


513  

there are many other ways to delete lines with specific string besides sed

除了sed之外,还有许多其他方法可以删除带有特定字符串的行

awk

awk

awk '!/pattern/' file > temp && mv temp file

Ruby (1.9+)

Ruby(1.9 +)

ruby -i.bak -ne 'print if not /test/' file

Perl

Perl

perl -ni.bak -e "print unless /pattern/" file

Shell (bash3.2+)

Shell(bash3.2 +)

while read -r linedo  [[ ! $line =~ pattern ]] && echo "$line"done <file > o mv o file

GNU grep

GNU grep

grep -v "pattern" file > temp && mv temp file

and of course sed (printing the inverse is faster than actual deletion. )

当然还有sed(打印逆比实际删除要快)。

sed -n '/pattern/!p' file 

#3


184  

You can use sed to replace lines in place in a file. However, it seems to be much slower than using grep for the inverse into a second file and then moving the second file over the original.

您可以使用sed替换文件中的行。然而,这似乎比使用grep将逆文件转换为第二个文件,然后将第二个文件移动到原来的文件上要慢得多。

e.g.

如。

sed -i '/pattern/d' filename      

or

grep -v "pattern" filename > filename2; mv filename2 filename

The first command takes 3 times longer on my machine anyway.

在我的机器上,第一个命令要多花3倍的时间。

#4


48  

The easy way to do it, with GNU sed:

简单的方法,GNU sed:

sed --in-place '/some string here/d' yourfile

#5


24  

You may consider using ex (which a standard UNIX command-based editor):

您可以考虑使用ex(一个标准的基于UNIX命令的编辑器):

ex +g/match/d -cwq file

where:

地点:

  • + executes given Ex command (man ex), same as -c which executes wq (write and quit)
  • +执行给定的Ex命令(man Ex),与执行wq(写和退出)的-c相同
  • g/match/d - Ex command to delete lines with given match, see: Power of g
  • g/match/d - Ex命令删除给定匹配的行,参见:g的幂

Above example is POSIX-compliant method for in-place editing a file as per this post at Unix.SE and POSIX specifications for ex.

上面的示例是与posix兼容的方法,用于根据本文在Unix中编辑文件。SE和POSIX规格为ex。


The difference with sed is that:

与sed的不同之处在于:

sed is a Stream EDitor, not a file editor.BashFAQ

sed是流编辑器,而不是文件编辑器。bashfaq

unless you enjoy unportable code, I/O overhead and some other bad side effects. So basically some parameters (such as in-place/-i) are non-standard FreeBSD extensions and may not be available on other operating systems.

除非您喜欢不可移植的代码、I/O开销和其他一些不好的副作用。因此,基本上有些参数(如in-place/-i)是非标准的FreeBSD扩展,可能在其他操作系统上不可用。

#6


12  

To get a inplace like result with grep you can do this:

要获得类似于grep的inplace的结果,你可以这样做:

echo "$(grep -v "pattern" filename)" >filename

#7


12  

I was struggling with this on Mac. Plus, I needed to do it using variable replacement.So I used:

我在Mac上遇到了这个问题。另外,我需要用变量替换。所以我使用:

sed -i '' "/$pattern/d" $file

sed -i "/$pattern/d" $file。

where $file is file where deletion is needed and $pattern is the pattern to be matched for deletion.Picked the '' from this comment.The thing to note here is use of double quotes in "/$pattern/d". Variable won't work when we use single quote.

其中$file是需要删除的文件,$pattern是要进行删除匹配的模式。摘自这条评论。这里要注意的是在“/$pattern/d”中使用双引号。当我们使用单引号时,变量将不起作用。

#8


9  

I have made a small benchmark with a file which contains approximately 345 000 lines. The way with grep seems to be around 15 times faster than the sed method in this case.

我用一个包含大约34.5万行的文件做了一个小的基准测试。在这种情况下,使用grep的方法似乎比sed方法快15倍。

I have tried both with and without the setting LC_ALL=C, it does not seem change the timings significantly. The search string (CDGA_00004.pdbqt.gz.tar) is somewhere in the middle of the file.

我已经尝试过了,没有设置LC_ALL=C,它似乎没有明显的改变时间。搜索字符串(CDGA_00004.pdbqt.gz.tar)位于文件的中间位置。

Here are the commands and the timings:

以下是命令和时间安排:

time sed -i "/CDGA_00004.pdbqt.gz.tar/d" /tmp/input.txtreal    0m0.711suser    0m0.179ssys     0m0.530stime perl -ni -e 'print unless /CDGA_00004.pdbqt.gz.tar/' /tmp/input.txtreal    0m0.105suser    0m0.088ssys     0m0.016stime (grep -v CDGA_00004.pdbqt.gz.tar /tmp/input.txt > /tmp/input.tmp; mv /tmp/input.tmp /tmp/input.txt )real    0m0.046suser    0m0.014ssys     0m0.019s

#9


8  

SED:

对话:

AWK:

AWK:

GREP:

GREP:

#10


7  

You can use this also

你也可以用这个。

 grep -v 'pattern' filename

here -v will print only other than your pattern(that means Invert match)

这里-v将只打印除您的模式之外的内容(这意味着反向匹配)

#11


2  

echo -e "/thing_to_delete\ndd\033:x\n" | vim file_to_edit.txt

echo -e "/thing_to_delete\ndd\033:x\n" | vim file_to_edit.txt。

#12


2  

perl -i    -nle'/regexp/||print' file1 file2 file3perl -i.bk -nle'/regexp/||print' file1 file2 file3

The first command edits the file(s) inplace (-i).

第一个命令编辑inplace (-i)中的文件。

The second command does the same thing but keeps a copy or backup of the original file(s) by adding .bk to the file names (.bk can be changed to anything).

第二个命令执行相同的操作,但是通过在文件名(. bk)中添加.bk来保存原始文件的副本或备份。bk可以被改变成任何东西)。

#13


0  

Just in case someone wants to do it for exact matches of strings, you can use -w flag in grep, w for whole. That is, for example if you want to delete the lines that have number 11 but keep the lines with number 111:

为了防止有人想要精确匹配字符串,您可以在grep中使用-w标志,w代表整个。例如,如果你想删除有11号的行但是保留有111号的行:

-bash-4.1$ head file111111-bash-4.1$ grep -v "11" file1-bash-4.1$ grep -w -v "11" file1111

Also works with -f flag if you want to exclude several exact patterns at once. If "blacklist" is a file with several patterns on each line that you want to delete from "file":

如果您想同时排除几个确切的模式,也可以使用-f标志。如果“黑名单”是从“file”中删除的每个行上都有多个模式的文件:

grep -w -v -f blacklist file

#14


0  

cat filename | grep -v "pattern" > filename

#1


1895  

To remove the line and print the output to standard out:

将输出移去并打印到标准输出:

sed '/pattern to match/d' ./infile

To directly modify the file:

直接修改文件:

sed -i '/pattern to match/d' ./infile

To directly modify the file (and create a backup):

直接修改文件(并创建备份):

sed -i.bak '/pattern to match/d' ./infile

For Mac OS X users:

Mac OS X用户:

sed -i '' '/pattern/d' ./infile

#2


513  

there are many other ways to delete lines with specific string besides sed

除了sed之外,还有许多其他方法可以删除带有特定字符串的行

awk

awk

awk '!/pattern/' file > temp && mv temp file

Ruby (1.9+)

Ruby(1.9 +)

ruby -i.bak -ne 'print if not /test/' file

Perl

Perl

perl -ni.bak -e "print unless /pattern/" file

Shell (bash3.2+)

Shell(bash3.2 +)

while read -r linedo  [[ ! $line =~ pattern ]] && echo "$line"done <file > o mv o file

GNU grep

GNU grep

grep -v "pattern" file > temp && mv temp file

and of course sed (printing the inverse is faster than actual deletion. )

当然还有sed(打印逆比实际删除要快)。

sed -n '/pattern/!p' file 

#3


184  

You can use sed to replace lines in place in a file. However, it seems to be much slower than using grep for the inverse into a second file and then moving the second file over the original.

您可以使用sed替换文件中的行。然而,这似乎比使用grep将逆文件转换为第二个文件,然后将第二个文件移动到原来的文件上要慢得多。

e.g.

如。

sed -i '/pattern/d' filename      

or

grep -v "pattern" filename > filename2; mv filename2 filename

The first command takes 3 times longer on my machine anyway.

在我的机器上,第一个命令要多花3倍的时间。

#4


48  

The easy way to do it, with GNU sed:

简单的方法,GNU sed:

sed --in-place '/some string here/d' yourfile

#5


24  

You may consider using ex (which a standard UNIX command-based editor):

您可以考虑使用ex(一个标准的基于UNIX命令的编辑器):

ex +g/match/d -cwq file

where:

地点:

  • + executes given Ex command (man ex), same as -c which executes wq (write and quit)
  • +执行给定的Ex命令(man Ex),与执行wq(写和退出)的-c相同
  • g/match/d - Ex command to delete lines with given match, see: Power of g
  • g/match/d - Ex命令删除给定匹配的行,参见:g的幂

Above example is POSIX-compliant method for in-place editing a file as per this post at Unix.SE and POSIX specifications for ex.

上面的示例是与posix兼容的方法,用于根据本文在Unix中编辑文件。SE和POSIX规格为ex。


The difference with sed is that:

与sed的不同之处在于:

sed is a Stream EDitor, not a file editor.BashFAQ

sed是流编辑器,而不是文件编辑器。bashfaq

unless you enjoy unportable code, I/O overhead and some other bad side effects. So basically some parameters (such as in-place/-i) are non-standard FreeBSD extensions and may not be available on other operating systems.

除非您喜欢不可移植的代码、I/O开销和其他一些不好的副作用。因此,基本上有些参数(如in-place/-i)是非标准的FreeBSD扩展,可能在其他操作系统上不可用。

#6


12  

To get a inplace like result with grep you can do this:

要获得类似于grep的inplace的结果,你可以这样做:

echo "$(grep -v "pattern" filename)" >filename

#7


12  

I was struggling with this on Mac. Plus, I needed to do it using variable replacement.So I used:

我在Mac上遇到了这个问题。另外,我需要用变量替换。所以我使用:

sed -i '' "/$pattern/d" $file

sed -i "/$pattern/d" $file。

where $file is file where deletion is needed and $pattern is the pattern to be matched for deletion.Picked the '' from this comment.The thing to note here is use of double quotes in "/$pattern/d". Variable won't work when we use single quote.

其中$file是需要删除的文件,$pattern是要进行删除匹配的模式。摘自这条评论。这里要注意的是在“/$pattern/d”中使用双引号。当我们使用单引号时,变量将不起作用。

#8


9  

I have made a small benchmark with a file which contains approximately 345 000 lines. The way with grep seems to be around 15 times faster than the sed method in this case.

我用一个包含大约34.5万行的文件做了一个小的基准测试。在这种情况下,使用grep的方法似乎比sed方法快15倍。

I have tried both with and without the setting LC_ALL=C, it does not seem change the timings significantly. The search string (CDGA_00004.pdbqt.gz.tar) is somewhere in the middle of the file.

我已经尝试过了,没有设置LC_ALL=C,它似乎没有明显的改变时间。搜索字符串(CDGA_00004.pdbqt.gz.tar)位于文件的中间位置。

Here are the commands and the timings:

以下是命令和时间安排:

time sed -i "/CDGA_00004.pdbqt.gz.tar/d" /tmp/input.txtreal    0m0.711suser    0m0.179ssys     0m0.530stime perl -ni -e 'print unless /CDGA_00004.pdbqt.gz.tar/' /tmp/input.txtreal    0m0.105suser    0m0.088ssys     0m0.016stime (grep -v CDGA_00004.pdbqt.gz.tar /tmp/input.txt > /tmp/input.tmp; mv /tmp/input.tmp /tmp/input.txt )real    0m0.046suser    0m0.014ssys     0m0.019s

#9


8  

SED:

对话:

AWK:

AWK:

GREP:

GREP:

#10


7  

You can use this also

你也可以用这个。

 grep -v 'pattern' filename

here -v will print only other than your pattern(that means Invert match)

这里-v将只打印除您的模式之外的内容(这意味着反向匹配)

#11


2  

echo -e "/thing_to_delete\ndd\033:x\n" | vim file_to_edit.txt

echo -e "/thing_to_delete\ndd\033:x\n" | vim file_to_edit.txt。

#12


2  

perl -i    -nle'/regexp/||print' file1 file2 file3perl -i.bk -nle'/regexp/||print' file1 file2 file3

The first command edits the file(s) inplace (-i).

第一个命令编辑inplace (-i)中的文件。

The second command does the same thing but keeps a copy or backup of the original file(s) by adding .bk to the file names (.bk can be changed to anything).

第二个命令执行相同的操作,但是通过在文件名(. bk)中添加.bk来保存原始文件的副本或备份。bk可以被改变成任何东西)。

#13


0  

Just in case someone wants to do it for exact matches of strings, you can use -w flag in grep, w for whole. That is, for example if you want to delete the lines that have number 11 but keep the lines with number 111:

为了防止有人想要精确匹配字符串,您可以在grep中使用-w标志,w代表整个。例如,如果你想删除有11号的行但是保留有111号的行:

-bash-4.1$ head file111111-bash-4.1$ grep -v "11" file1-bash-4.1$ grep -w -v "11" file1111

Also works with -f flag if you want to exclude several exact patterns at once. If "blacklist" is a file with several patterns on each line that you want to delete from "file":

如果您想同时排除几个确切的模式,也可以使用-f标志。如果“黑名单”是从“file”中删除的每个行上都有多个模式的文件:

grep -w -v -f blacklist file

#14


0  

cat filename | grep -v "pattern" > filename