How to find lines matching a pattern and delete them?
Try sed
:
sed -i '/^HERE IT IS/d' <file>
WARNING: Its better to take a backup when using -i
switch of sed
:
sed -i.bak '/^HERE IT IS/d' <file>
The original file will remain as <file>.bak
and the modified file will be <file>
.
In addition to the very good grep
and sed
answers you've received, here are some other tools that can do the same thing:
A few Perl ways:
perl -ne '/^HERE IT IS/ || print' file > newfile perl -ne 'print if !/^HERE IT IS/' file > newfile perl -ne 'print unless /^HERE IT IS/' file > newfile
You can add the
-i
switch to any of the examples to edit the file in place:perl -i.bak -ne '/^HERE IT IS/ || print' file
(g)awk
awk '!/^HERE IT IS/' file > newfile
Newer versions (4.1.1 and later) of GNU
awk
(the defaultawk
on Linux) can also edit the file in place:gawk -i inplace '!/^HERE IT IS/' file
Shell (
bash
,zsh
,ksh
, probably others). This is kind of silly though, it can be done but other tools are better.while IFS= read -r line; do [[ $line =~ ^"HERE IT IS" ]] || printf "%s\n" "$line" done < file > newfile
I would use grep
to filter them out. For example :
grep -v "^HERE IT IS" infile > outfile
Then move outfile back to infile.