Re: Sed and redirection into the same file

David <[email protected]> Fri, 29 Sep 2006 16:08:37 +1000
Newsgroups gmane.org.user-groups.mlug.main
Message-ID <[email protected]>
On Friday 29 September 2006 15:53, Joshua Bassett wrote:
> Hi all,
>
> I was wondering if anyone could help me with a find/replace command I
> have been trying to get to work:
>
> for x in `grep -lr 'foo' app | grep -v "svn"`; do sed 's/foo/bar/'
> <${x} >${x}; done
>
> I want to replace the word 'foo' with the word 'bar' in all files in
> the 'app' directory', except for files in directories that contain
> 'svn' anywhere in the directory name.
>
> It all appears to work fine, but the problem is when redirecting the
> output back into the same file...I end up with empty files! For
> example, the following works fine:
>
> for x in `grep -lr 'foo' app | grep -v "svn"`; do sed 's/foo/bar/'
> <${x} >${x}.new; done
>
> Clearly it's something to do with the <${x} >${x} part. What am I missing
> here?
>
> Thanks in advance,
>
> Josh.

You can use perl.. simple.

for i in `grep -L svn *` ; do
        perl -pi -e 's/search/replace/g' $i
done

HIH.

David