Re: Sed and redirection into the same file
"Joshua Bassett" <[email protected]> Fri, 29 Sep 2006 16:45:25 +1000
| Newsgroups | gmane.org.user-groups.mlug.main |
|---|---|
| Message-ID | <[email protected]> |
Further reading on the perl find/replace solution yielded another tip for command line non-gurus like myself. Use: perl -pi.bak -e 's/foo/bar/g' file1 file2 file3 ... The .bak option to -i will backup the files before overwriting them. A useful safety net if you're making changes to important files. Josh. On 9/29/06, David <[email protected]> wrote: > On Friday 29 September 2006 16:08, David wrote: > > 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 > > Replying to my own mail... as I just noticed the directories. > > #!/bin/sh > files=`find app -type f \! -name \*svn\* -print` > for i in $files ; do > perl -pi -e 's/search/replace/g' $i > done > > HIH. > David > > _______________________________________________ > Melbourne Linux Users Group (Australia) > MLUG Web Site: http://www.mlug.org.au/ > Edit your settings: http://lists.mlug.org.au >