3

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.

1 Answer 1

3

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' *
1
  • 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, 2013 at 12:57

You must log in to answer this question.

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