views:

151

answers:

3

How do i find and replace a string on command line in multiple files on unix?

+5  A: 

there are many ways .But one of the answers would be:

find . -name '*.html' |xargs perl -pi -e 's/find/replace/g'
Vijay Sarathi
+2  A: 

I always did that with ed scripts or ex scripts.

for i in "$@"; do ex - "$i" << 'eof'; done
%s/old/new/
x
eof

The ex command is just the : line mode from vi.

DigitalRoss
A: 

with recent bash shell, and assuming you do not need to traverse directories

for file in *.txt
do
while read -r line
do
    echo ${line//find/replace} > temp        
done <"file"
mv temp "$file"
done