shell - How to delete from a text file, all lines that contain a specific string?

ID : 421

viewed : 132

Tags : shellsedtext-parsingin-placeshell





Top 5 Answer for shell - How to delete from a text file, all lines that contain a specific string?

vote vote

92

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

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

To directly modify the file – does not work with BSD sed:

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

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

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

To directly modify the file (and create a backup) – works with BSD and GNU sed:

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

85

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

AWK

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

Ruby (1.9+)

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

Perl

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

Shell (bash 3.2 and later)

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

GNU grep

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

And of course sed (printing the inverse is faster than actual deletion):

sed -n '/pattern/!p' file 
vote vote

70

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.

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.

vote vote

63

The easy way to do it, with GNU sed:

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

60

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

ex +g/match/d -cwq file 

where:

  • + executes given Ex command (man ex), same as -c which executes wq (write and quit)
  • g/match/d - Ex command to delete lines with given match, see: Power of g

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


The difference with sed is that:

sed is a Stream EDitor, not a file editor.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.

Top 3 video Explaining shell - How to delete from a text file, all lines that contain a specific string?







Related QUESTION?