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

Bob Proulx <[email protected]>
Newsgroups gmane.comp.gnu.utils.bugs
Message-ID <[email protected]>
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 [email protected] somefile; then
> >     sed --in-place 's/[email protected]/[email protected]/'
> >   fi
> 
> Or, more efficiently:
> 
> grep -r -q [email protected] /the/root/place | xargs sed --in-place s/foo@a/bar@b/

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 [email protected] /the/sed/place \
    | xargs -0 sed --in-place s/foo@a/bar@b/

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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.