Re: [gentoo-user] Multi-file search & replace of text

2010-03-01 Thread Helmut Jarausch
On 28 Feb, Stroller wrote:
> Hi there,
> 
> If I want to automagically replace text in a file, I can use `sed`. I don't 
> believe that `sed` can be invoked in such a way to change the file in place, 
> therefore two commands are necessary:
> 
>$ sed 's/Project Gutenberg/Wordsworth Classics/' foo > bar
>$ mv bar foo 
>$ 
> 
> Using `grep` I can search *recursively* through directories to find the text 
> I'm looking for. EG: `grep -R Gutenberg ~`
> 
> I would like to find every instance of $foo in a directory hierarchy and 
> replace it with $bar. 
> 
> Is there any tool that will combine all these operations for me?
> 
> If not, what is the best way to string together grep and sed so that they'll 
> do what I want?
> 

You might have a look at an old program which is still quite useful
The link in the following web page is broken!
http://centoshacker.com/kabir/utility/global-search-and-replace-using-the-fgres-utility.html
Download from
http://wiki.uni-konstanz.de/pitz/fgres.html

Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany



Re: [gentoo-user] Multi-file search & replace of text

2010-02-28 Thread stosss
On Sun, Feb 28, 2010 at 4:41 PM, Stroller
 wrote:
>
> On 28 Feb 2010, at 19:06, Etaoin Shrdlu wrote:
>>
>> On Sunday 28 February 2010, Stroller wrote:
>>>
>>> ...
>>>  $ sed 's/Project Gutenberg/Wordsworth Classics/' foo > bar
>>>  $ mv bar foo
>>>  $
>>
>> Have a look at sed's "-i" option.
>>
>>> Using `grep` I can search *recursively* through directories to find the
>>> text I'm looking for. EG: `grep -R Gutenberg ~`
>>>
>>> I would like to find every instance of $foo in a directory hierarchy and
>>> replace it with $bar.
>>>
>>> ...
>>
>> A starting point could be (after you make a backup of the whole tree)
>>
>> find /basedir -type f -exec sed -i 's/foo/bar/g' {} +
>
> Many thanks - that looks great!
>
> My only concern is that it is unreliable enough that you state the need to
> backup first. ;)

Why are you concerned about a backup? It is always good to do backups
before changing things. You never know when something might go wrong.
;)



Re: [gentoo-user] Multi-file search & replace of text

2010-02-28 Thread Etaoin Shrdlu
On Sunday 28 February 2010, Stroller wrote:

> > A starting point could be (after you make a backup of the whole tree)
> >
> > find /basedir -type f -exec sed -i 's/foo/bar/g' {} +
> 
> Many thanks - that looks great!
> 
> My only concern is that it is unreliable enough that you state the
> need to backup first. ;)

The problem is that with such a command it's very easy to screw up hundreds or 
thousands of files (depending how many you have in the directory tree) in a 
non-reversible way, for example due to a slight error in the sed command.

Hence the suggestion of backing up before trying. Alternatively, you can 
supply an extension to the -i option, as in -i.bak for example, to have sed 
create backup copies of the changed files (which you can then remove when 
you've made sure the changes have been successful).



Re: [gentoo-user] Multi-file search & replace of text

2010-02-28 Thread Stroller


On 28 Feb 2010, at 19:06, Etaoin Shrdlu wrote:

On Sunday 28 February 2010, Stroller wrote:

...
  $ sed 's/Project Gutenberg/Wordsworth Classics/' foo > bar
  $ mv bar foo
  $


Have a look at sed's "-i" option.

Using `grep` I can search *recursively* through directories to find  
the

text I'm looking for. EG: `grep -R Gutenberg ~`

I would like to find every instance of $foo in a directory  
hierarchy and

replace it with $bar.

...

A starting point could be (after you make a backup of the whole tree)

find /basedir -type f -exec sed -i 's/foo/bar/g' {} +


Many thanks - that looks great!

My only concern is that it is unreliable enough that you state the  
need to backup first. ;)


Stroller.





Re: [gentoo-user] Multi-file search & replace of text

2010-02-28 Thread Etaoin Shrdlu
On Sunday 28 February 2010, Stroller wrote:

> If I want to automagically replace text in a file, I can use `sed`. I don't
>  believe that `sed` can be invoked in such a way to change the file in
>  place, therefore two commands are necessary:
> 
>$ sed 's/Project Gutenberg/Wordsworth Classics/' foo > bar
>$ mv bar foo
>$

Have a look at sed's "-i" option.

> Using `grep` I can search *recursively* through directories to find the
>  text I'm looking for. EG: `grep -R Gutenberg ~`
> 
> I would like to find every instance of $foo in a directory hierarchy and
>  replace it with $bar.
> 
> Is there any tool that will combine all these operations for me?
> 
> If not, what is the best way to string together grep and sed so that
>  they'll do what I want?

A starting point could be (after you make a backup of the whole tree)

find /basedir -type f -exec sed -i 's/foo/bar/g' {} +