LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   search/replace in many files (https://www.linuxquestions.org/questions/linux-general-1/search-replace-in-many-files-349417/)

allelopath 08-02-2005 09:05 PM

search/replace in many files
 
What is the best way to recursively search thru a directory structure and search and replace text in files?

For example, if I wanted to search thru all files with extension .txt and replace all occurences of the string "red" with "blue".

Matir 08-02-2005 09:21 PM

And write it back out to the same file?
Code:

#!/bin/bash
SEARCH=red
REPLACE=blue


for file in `find . -name '*.txt'`
    do sed 's/$SEARCH/$REPLACE' < $file > $file.new
    mv $file.new $file
done

Test it on a copy first. :)


All times are GMT -5. The time now is 01:41 PM.