Gentoo Archives: gentoo-user

From: Etaoin Shrdlu <shrdlu@×××××××××××××.org>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Multi-file search & replace of text
Date: Sun, 28 Feb 2010 20:03:30
Message-Id: 201002281906.16284.shrdlu@unlimitedmail.org
In Reply to: [gentoo-user] Multi-file search & replace of text by Stroller
1 On Sunday 28 February 2010, Stroller wrote:
2
3 > If I want to automagically replace text in a file, I can use `sed`. I don't
4 > believe that `sed` can be invoked in such a way to change the file in
5 > place, therefore two commands are necessary:
6 >
7 > $ sed 's/Project Gutenberg/Wordsworth Classics/' foo > bar
8 > $ mv bar foo
9 > $
10
11 Have a look at sed's "-i" option.
12
13 > Using `grep` I can search *recursively* through directories to find the
14 > text I'm looking for. EG: `grep -R Gutenberg ~`
15 >
16 > I would like to find every instance of $foo in a directory hierarchy and
17 > replace it with $bar.
18 >
19 > Is there any tool that will combine all these operations for me?
20 >
21 > If not, what is the best way to string together grep and sed so that
22 > they'll do what I want?
23
24 A starting point could be (after you make a backup of the whole tree)
25
26 find /basedir -type f -exec sed -i 's/foo/bar/g' {} +

Replies

Subject Author
Re: [gentoo-user] Multi-file search & replace of text Stroller <stroller@××××××××××××××××××.uk>