Deleting lines in a file 
Author Message
 Deleting lines in a file

Can someone tell me how to delete all lines in a file that contain a certain
string in the line?  I'm not even sure that awk is the best way to go or should
I use sed or vi.  Thanks



Mon, 08 Jul 2002 03:00:00 GMT  
 Deleting lines in a file

Quote:

>Can someone tell me how to delete all lines in a file that contain a
>certain string in the line?  I'm not even sure that awk is the best
>way to go or should I use sed or vi.  Thanks

awk has a contains/match operator ~ so one can do this easily:

awk '$0 ~ "string" {next} {print}' infile > outfile

or using negative logic:

awk '$0 !~ "string" {print}' infile > outfile

or even shorter:

awk '$0 !~ "string" ' infile > outfile

but this is a regular expression match, so it might be better/best to
use the index() function which does a string match returning the index
where the string is found, otherwise zero.

awk '!index($0,"string")' infile > outfile

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 08 Jul 2002 03:00:00 GMT  
 Deleting lines in a file

Quote:

>Can someone tell me how to delete all lines in a file that
>contain a certain string in the line?  I'm not even sure
>that awk is the best way to go or should I use sed or vi.

Well, you asked in an awk newsgroup, so you get an awk
answer first.

awk '{ if (! /a certain string/) print }' file

That said, read about grep -v in the manual.

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful



Mon, 08 Jul 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Preserving EAs when deleting lines from a file.

2. Deleting lines of a file?

3. How delete lines from a file?

4. deleting newline from last line in file

5. how to delete the a line in a file

6. Deleting and appending lines in ASCII files

7. Deleting a number of lines from a .txt file

8. deleting top line from a file

9. delete one line of a file???

10. Delete a certain line from a file

11. delete lines in file

12. Deleting first line of a file

 

 
Powered by phpBB® Forum Software