Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following command replaces every occurence of the word from in any file of the current directory or its subdirectories with to:

ack-grep -l --print0 --text from | xargs -0 -n 1 sed -i -e 's/from/to/g'

I got that from this thread.

How do I replace a string including multiple words instead of one word? E.g. replace laughing babies by smiling little children.

I tried the following but these did not work:

ack-grep -l --print0 --text 'laughing babies' | xargs -0 -n 1 sed -i -e "s/'laughing babies'/'smiling little children'/g"
ack-grep -l --print0 --text 'laughing babies' | xargs -0 -n 1 sed -i -e 's/"laughing babies"/"smiling little children"/g'

Don't know if it matters but I'm on Ubuntu 11.10.

share|improve this question
up vote 2 down vote accepted

You actually don't need the quotes, sed can deal with spaces and the quotes are taken as being part of the pattern (unless you escape them). Just do this:

ack-grep -l --print0 --text 'laughing babies' | xargs -0 -n 1 sed -i -e 's/laughing babies/smiling little children/g'

I don't see why you want the grep part though, you could just run sed directly on all files of the directory, sed is fast:

sed -i -e 's/laughing babies/smiling little children/g' *
share|improve this answer
Because ack-grep omits certain files like those in .git in its search. But that sed command is good to know nevertheless, thanks! – Bentley4 Mar 19 '13 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .