xz02
asked on
delete lines containing a pattern?
HI, how can delete all the lines contains certain string using sed or other tools?
Shane
Shane
As ozo said, you can do with sed, grep, perl, awk etc.
Personally I'd use grep -v (find all lines not containing string).
cat infile | grep -v 'certain string' >outfile ; mv -r outfile infile
But there's lots and lots of ways of doing this.
Personally I'd use grep -v (find all lines not containing string).
cat infile | grep -v 'certain string' >outfile ; mv -r outfile infile
But there's lots and lots of ways of doing this.
ASKER
Thank you very much. It works. Suppose I want to do the thing for
all *.dat files under the directory, what should I do then?
Thanks again.
all *.dat files under the directory, what should I do then?
Thanks again.
perl -i -ne 'print unless /certain string/' *.dat
or in sh
for name in *.dat ;do
grep -v 'certain string' <$name >outfile
mv outfile $name
done
or in csh
foreach name ( *.dat )
sed -e '/certain string/ d' <$name >outfile
mv outfile $name
end
or in sh
for name in *.dat ;do
grep -v 'certain string' <$name >outfile
mv outfile $name
done
or in csh
foreach name ( *.dat )
sed -e '/certain string/ d' <$name >outfile
mv outfile $name
end
ping
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
grep -v 'certain string' < oldfile > newfile
perl -i -ne 'print unless /certain string/' file
. .