[lug] Another sed question.

rm at fabula.de rm at fabula.de
Fri Apr 1 02:36:31 MST 2005

On Thu, Mar 31, 2005 at 05:44:25PM -0700, David Anselmi wrote:
> Jeff Schroeder wrote:
> >Shannon asked:
> >
> >>I would like to locate a string of characters in a file, and delete
> >>the entire line that contains that string.
> Do you have any books on the shell or Unix?  That's a pretty basic 
> question.  There are alos many tutorials on the web, and even this book:
> http://www.icon.co.za/~psheer/book/index.html.gz
> That has chapters on regexs, sed, and a bunch of other stuff you might 
> find useful.  You'd be much better off reading a book about the shell or 
> sed (or even vi, which can do this as well) than asking your questions 
> one by one here. 

Oh, c'mon. I love these little questions: always entertaining to see all those
different solutions come in ... (o.k. Linux is all about educated computer
use, and reading a good intro on shell/sysadmin stuff never hurts).

> In reading you'll pick up the answers to your next 3 
> questions while looking up the first.
> >Rather than using sed, I'd use grep:
> Rather than grep, if you want the results in the same file, I'd use perl:
> perl -line '/match/ or print' file

Hmm, interesting - all these solve different problems :-/
Shannon wasn't really clear about what exactly is wanted: should the _first_
occurence of a matched line be deleted? Then the given solution is correct.
Grep can't really modify/substitute a file, it merely prints a match. It would
be possible to have grep return all lines _not_ matching and then redirecting
this to an output file, but _never_ try this: grep foo file.txt  > file.txt
This will erase the file!
The perl solution will kill all lines that match a given pattern.

cheers RalfD

> vi might be a good choice to get the results in the same file, since you 
> might be more familiar with it than perl (and might not have perl or its 
> docs handy).
> Dave
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug

More information about the LUG mailing list