views:

71

answers:

2

is there a way to search and replace a string using single unix command grep recusrsively in multiple directories? i know it can be done by using the combination of find with other utilities like sed perl etc.but is there a way where we can use only grep for doing this on unix command line?

+1  A: 

I don't think that only grep would work here; involving sed and other utilities will be much more easier, than just grep

Himanshu
A: 

one way, if you have GNU find and bash shell

find /path -type f -iname "*.txt" | while read -r FILE
do
    while read -r LINE
    do
       case "$LINE" in
         *WORD_TO_SEARCH* ) LINE=${LINE//WORD_TO_SEARCH/REPLACE};;
       esac
       echo "$LINE" >> temp
    done < "$FILE"
    mv temp "$FILE"
done