How to delete from a text file, all lines that contain a specific string?
How would I use sed to delete all lines in a text file that contain a specific string?
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
Read more… Read less…
There are many other ways to delete lines with specific string besides
awk '!/pattern/' file > temp && mv temp file
ruby -i.bak -ne 'print if not /test/' file
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
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
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 -i '/pattern/d' filename
grep -v "pattern" filename > filename2; mv filename2 filename
The first command takes 3 times longer on my machine anyway.
The easy way to do it, with GNU
sed --in-place '/some string here/d' yourfile
You may consider using
ex (which is a standard Unix command-based editor):
ex +g/match/d -cwq file
+executes given Ex command (
man ex), same as
wq(write and quit)
g/match/d- Ex command to delete lines with given
match, see: Power of g
The difference with
sed is that:
sedis 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.
I was struggling with this on Mac. Plus, I needed to do it using variable replacement.
So I used:
sed -i '' "/$pattern/d" $file
$file is the file where deletion is needed and
$pattern is the pattern to be matched for deletion.
I 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 quotes.