Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.