public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] Multi-file search & replace of text
@ 2010-02-28 19:05 Stroller
  2010-02-28 19:06 ` Etaoin Shrdlu
  2010-03-01  8:46 ` Helmut Jarausch
  0 siblings, 2 replies; 6+ messages in thread
From: Stroller @ 2010-02-28 19:05 UTC (permalink / raw
  To: gentoo-user

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?

Many thanks for reading,

Stroller.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [gentoo-user] Multi-file search & replace of text
  2010-02-28 19:05 [gentoo-user] Multi-file search & replace of text Stroller
@ 2010-02-28 19:06 ` Etaoin Shrdlu
  2010-02-28 21:41   ` Stroller
  2010-03-01  8:46 ` Helmut Jarausch
  1 sibling, 1 reply; 6+ messages in thread
From: Etaoin Shrdlu @ 2010-02-28 19:06 UTC (permalink / raw
  To: gentoo-user

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' {} +



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [gentoo-user] Multi-file search & replace of text
  2010-02-28 19:06 ` Etaoin Shrdlu
@ 2010-02-28 21:41   ` Stroller
  2010-02-28 21:44     ` Etaoin Shrdlu
  2010-02-28 21:57     ` stosss
  0 siblings, 2 replies; 6+ messages in thread
From: Stroller @ 2010-02-28 21:41 UTC (permalink / raw
  To: gentoo-user


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.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [gentoo-user] Multi-file search & replace of text
  2010-02-28 21:41   ` Stroller
@ 2010-02-28 21:44     ` Etaoin Shrdlu
  2010-02-28 21:57     ` stosss
  1 sibling, 0 replies; 6+ messages in thread
From: Etaoin Shrdlu @ 2010-02-28 21:44 UTC (permalink / raw
  To: gentoo-user

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).



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [gentoo-user] Multi-file search & replace of text
  2010-02-28 21:41   ` Stroller
  2010-02-28 21:44     ` Etaoin Shrdlu
@ 2010-02-28 21:57     ` stosss
  1 sibling, 0 replies; 6+ messages in thread
From: stosss @ 2010-02-28 21:57 UTC (permalink / raw
  To: gentoo-user

On Sun, Feb 28, 2010 at 4:41 PM, Stroller
<stroller@stellar.eclipse.co.uk> 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.
;)



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [gentoo-user] Multi-file search & replace of text
  2010-02-28 19:05 [gentoo-user] Multi-file search & replace of text Stroller
  2010-02-28 19:06 ` Etaoin Shrdlu
@ 2010-03-01  8:46 ` Helmut Jarausch
  1 sibling, 0 replies; 6+ messages in thread
From: Helmut Jarausch @ 2010-03-01  8:46 UTC (permalink / raw
  To: gentoo-user

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-03-01  8:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-28 19:05 [gentoo-user] Multi-file search & replace of text Stroller
2010-02-28 19:06 ` Etaoin Shrdlu
2010-02-28 21:41   ` Stroller
2010-02-28 21:44     ` Etaoin Shrdlu
2010-02-28 21:57     ` stosss
2010-03-01  8:46 ` Helmut Jarausch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox