bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sed --in-place bug (updates mtime of file even if no changes are mad


From: Bob Proulx
Subject: Re: sed --in-place bug (updates mtime of file even if no changes are made)
Date: Mon, 12 Dec 2011 19:46:46 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

John Cowan wrote:
> Bob Proulx scripsit:
> > BTW...  Whenever I need to do something like this I always grep first
> > and edit only if the grep tells me that I need to do something.  That
> > way the file isn't modified if there isn't any change to it.
> > 
> >   if grep -q address@hidden somefile; then
> >     sed --in-place 's/address@hidden/address@hidden/'
> >   fi
> 
> Or, more efficiently:
> 
> grep -r -q address@hidden /the/root/place | xargs sed --in-place 
> s/address@hidden/address@hidden/

Clever!  But the smallest error can leave things not quite working.
(And I left an error in my example too by not including the filename
for sed.)  You left out the grep -l option to have it print out file
names.  Swap -l for -q.  And if going this route then use grep and
xargs --null options to prduce zero terminated output and xargs so
that it can handle arbitrary filenames including those that have
whitespace in them.

  grep -lZr address@hidden /the/sed/place \
    | xargs -0 sed --in-place s/address@hidden/address@hidden/

I like it!  But usually I just want to do one file at a time and so
will probably stick with the long shell script form for the clarity
when doing those special tasks.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]