Link to home
Start Free TrialLog in
Avatar of xz02
xz02

asked on

delete lines containing a pattern?

HI, how can delete all the lines contains certain string using sed or other tools?
Shane
Avatar of ozo
ozo
Flag of United States of America image

sed -e '/certain string/ d' < oldfile > newfile
grep -v 'certain string' < oldfile > newfile
perl -i -ne 'print unless /certain string/' file
 . .
Avatar of richrussell
richrussell

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.
Avatar of xz02

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.
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
ASKER CERTIFIED SOLUTION
Avatar of ksb
ksb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial