Blog Archives

Quick Replace

This one is going to be short, it is something that would fit better under commandlinefu, tips and tricks!

What do we want to do?

Look for al the cpp files under a directory and modify all the text with foo to bar.

How to do it?

First we find all the cpp files under the current directory
find . -name "*.cpp" -print

And a possible command to replace from a file is using sed.
sed -i 's/foo/bar/g'

We just have to connect those two together to replace in all the files with a name ending in .cpp, that’s what pipes are for.
find . -name "*.cpp" -print | xargs sed -i 's/foo/bar/g'

Ta-Dah

Leave a comment

November 1, 2014 · 6:35 pm