One-line global string replace in all files with sed (or awk?)

markzero mark at darklogik.org
Thu Jan 27 22:07:54 PST 2005


> My thanks to all who replied.  I ended up using this form (I don't
> recall who suggested it):
> 
> find . -type f | xargs sed -i '' -e 's/foo/bar/g'
> 
> One problem, though:  It appears that sed touches every file, resetting
> the last modification time, even if it didn't actually change anything.
> This reset the last modification dates for every file on my site, which
> wasn't much fun.  Is there another command I could put between find and
> xargs that would filter only the names of files containing the string?
> (grep would do it, but grep outputs the lines found to stdout, so that
> won't do.)
> 

Completely off the top of my head:

#!/bin/sh
 
 for i in `find $PWD -type f`;   
 do
    grep foo $i 1>/dev/null;

    if [ $? -ne 0 ] then
       #do something with sed here
    fi
done;

Not *exactly* a one liner but it *could* be if you want to make it less
readable...

:)

Mark

-- 
PGP: http://www.darklogik.org/pub/pgp/pgp.txt
B776 43DC 8A5D EAF9 2126 9A67 A7DA 390F DEFF 9DD1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 825 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20050128/b6d9cca2/attachment.bin


More information about the freebsd-questions mailing list