FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo User

 
 
LinkBack Thread Tools
 
Old 02-28-2010, 06:05 PM
Stroller
 
Default Multi-file search & replace of text

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.
 
Old 02-28-2010, 06:06 PM
Etaoin Shrdlu
 
Default Multi-file search & replace of text

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' {} +
 
Old 02-28-2010, 08:41 PM
Stroller
 
Default Multi-file search & replace of text

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.
 
Old 02-28-2010, 08:44 PM
Etaoin Shrdlu
 
Default Multi-file search & replace of text

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).
 
Old 02-28-2010, 08:57 PM
stosss
 
Default Multi-file search & replace of text

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.
 
Old 03-01-2010, 07:46 AM
Helmut Jarausch
 
Default Multi-file search & replace of text

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
 

Thread Tools




All times are GMT. The time now is 11:17 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org