Reputation: 10971
I'm refactoring some code, and I decided to replace one name by another, let's say foo
by bar
. They appear in multiple .cc
and .h
files, so I would like to change from:
Foo key();
to
Bar key();
that's it, replace all the occurrences of Foo
by Bar
in Unix. And the files are in the same directory. I thought about
sed -e {'s/Foo/Bar/g'}
but I'm unsure if that's going to work.
Upvotes: 2
Views: 1430
Reputation: 25524
I like Jaypal's sed command. It useds \b
to ensure that you only replace full words (Foo
not Foobar
) and it makes backup files in case something went wrong.
However, if all of your files are not in one directory, then you will need to use a more sophisticated method to list them all. Use the find command to send them all to sed:
find . -print0 -regex '.*\.\(cc\|h\)' | xargs -0 sed -i'.bak' 's/\bFoo\b/Bar/g'
Upvotes: 1
Reputation: 4228
I don't use sed alot, but iF you have access to Perl on the command line (which many unix's do) you can do:
perl -pi -e 's/Foo key/Bar key/g' `find ./ -name '*.h' -o -name '*.cc'`
This will find (recursively) all files in the current directory ending with .h or .cc and then use Perl to replace 'Foo key' with 'Bar key' in each file.
Upvotes: 1
Reputation: 4334
I would use sed:
sed -i.bak -e '/Foo/ s//Bar/g' /path/to/dir/*.cc
Repeat for the *.h files
Upvotes: 1
Reputation: 77105
This should do the trick:
sed -i'.bak' 's/\bFoo\b/Bar/g' *.files
Upvotes: 8
Reputation: 1996
You probably have perl installed (if its UNIX), so here's something that should work for you:
perl -e "s/Foo/Bar/g;" -pi.save $(find path/to/DIRECTORY -type f)
Note, this provides a backup of the original file, if you need that as a bit of insurance.
Otherwise, you can do what @Kevin mentioned and just use an IDE refactoring feature.
Note: I just saw you're using Vim, here's a quick tutorial on how to do it
Upvotes: 0