I’m trying to search and replace a string in all files matched by grep:
grep -n 'foo' * will give me output in the form:
[filename]:[line number]:[text]
For each file returned by grep, I’d like to modify the file by replacing foo with bar.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Do you mean search and replace a string in all files matched by grep?
Edit
Since this seems to be a fairly popular question thought I’d update.
Nowadays I mostly use
ack-grepas it’s more user-friendly. So the above command would be:To handle whitespace in file names you can run:
you can do more with
ack-grep. Say you want to restrict the search to HTML files only:And if white space is not an issue it’s even shorter: