Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I use the grep command to search for a string in many files. Is there something similar to "search and replace"?

share|improve this question
add comment

3 Answers 3

up vote 14 down vote accepted

You are looking for the sed command. For example, to replace the "dog" with "cat" in all text files in the current directory:

sed -i 's/dog/cat/' *.txt
share|improve this answer
1  
Simple and nice. –  Luis Alvarado Feb 2 '12 at 14:29
add comment

sed comes to mind. Example:

sed s/cat/dog/ <input >output

This searches for cat in a line and puts dog on it's place in the file input and writes to file output.

share|improve this answer
add comment

sed is the right tool; but, as an alternative, you can even use ex commands in vim:

vim -c 'args <files> | argdo %s/cat/dog/g | x'

where <files> is the list of files or patterns in which you want to make the substitution.

share|improve this answer
add comment

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.