Grep, Sed and Grasp: Replace strings in files

This week, I was refactoring some of the Javascript code, which involved renaming files, and then amending the code that was requiring those files. The project uses requirejs, so the filename is kinda pivotal. I had read about Grasp and saw this as an opportunity to do some refactoring voodoo. Grasp is very powerful and there is ample documentation, but when you don’t have enough time, you want to look at some examples that tell you exactly what to do. Rails API docs have spoiled me for good. Grasp still needs to be conquered, perhaps another time.

Here’s the one-liner that I used to accomplish this task. I used it as part of a script so the values were dynamically substituted:

grep -rl '<oldString>' <path> | xargs sed -i '' 's/<oldString>/<newString>/g' $1

<oldString> is the string you want to replace
<newString> is the string you want to replace with
<path> is where you want grep to start working from

Grep recursively (-r) finds the files (-l) and pipes the file path in to xargs which executes the sed command for file it receives. $1 references the file path received via xargs. This one-liner doesn’t create a backup file for every file changed, however, if you want to create a backup file as well in the same directory as the file changed, you can use the following, slightly different one-liner:

grep -rl '<oldString>' <path> | xargs sed -i.bak 's/<oldString>/<newString>/g' $1

If I knew grasp well, I’d have used that instead, but for now, grep and sed are good enough.

About andhapp

Namastey (Hello) Stranger, This is not the geekiest blog on the planet yet it is not the dumbest one either. I am a small time developer who likes to explore new technologies. I usually program in Java, Groovy and Ruby and take keen interest in the surrounding geeky developments.
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>