How do I replace text string in many files at once?

Recently I came across nice littile nifty utility called replace. replace changes strings in place in files or on the standard input. Uses a finite state machine to match longer strings first. Can be used to swap strings. Sure sed (stream editor) can be use to replace any string in files but replace command is easy to use and quicker for simpler replacement. Syntax is as follows:

replace OLD-STRING NEW-STRING <INPUT-FILE >OUTPUT-FILE

For example to replace all occurrences of word UNIX with Linux
$ replace UNIX Linux < oldfile > newfile

replace can be used in a pipeline as follows:
$ cat /etc/passwd | replace : '|'

It also supports few special characters in string replacement:
\^ : Match start of line.
$ : Match end of line.

For example replace all IP address 192.168.1.2 start of line, we can use it as follows:
$ replace \^192.168.1.2 192.168.5.10 < oldfile > newfile

However replace does not understand regular expression :(, if you need regular expression it is better you go for sed or perl :D

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 11 comments… read them below or add one }

1 Joe 11.12.05 at 11:30 pm

To use perl, just remember Perl Pie!

perl -p -i -e ’s/hello/goodbye/g’ textfile.txt

2 LinuxTitli 11.12.05 at 11:47 pm

Joe, very nice. Your tip sound yummy :) thanks for sharing with us

3 Anonymous 11.30.05 at 9:34 am

http://www.debian-administration.org/articles/298 has a fine article and discussion on Perl Pie.

4 Anonymous 12.12.05 at 12:13 am

what about this line:

perl -p -i -e ’s/|00000000.00|/||/g’ myfile.txt

I want to replace |00000000.00| with ||

I get a compilation error.

5 nixcraft 12.12.05 at 1:41 am

You need to write it as follows:

perl -p -i -e ’s/|00000000.00|/||/g’ myfile.txt

|| got some special meaning (regex) | will disable it

6 Anonymous 12.12.05 at 9:02 pm

This does the trick perfectly! Thanks for sharing this special character technique nixcraft!

7 walter 08.09.07 at 6:59 am

or try this…
first make a bash script, ‘fixer.sh’


#!/bin/bash
replace CHANGEFROM CHANGETO $1.tmp
rm $1
mv $1.tmp $1

now run this command line…

$ grep CHANGEFROM |cut -d':' -f1 |xargs -n 1 fixer.sh

the results is that all files in the directory (or whatever you grep for) will be changed automagically.

just make sure the grep doesn’t include the fixer script itself, or it will die half-way through changing when execute permissions are reset!

;)

8 Raj 04.10.08 at 1:25 pm

thanks for sharing this info

9 lefty.crupps 06.16.08 at 4:28 pm

Am I the only one who sees no difference in these?

>> perl -p -i -e ’s/|00000000.00|/||/g’ myfile.txt
>> I want to replace |00000000.00| with ||
>> I get a compilation error.

— —
>> You need to write it as follows:
>> perl -p -i -e ’s/|00000000.00|/||/g’ myfile.txt

10 Eden 02.11.09 at 12:21 am

Cant see the diference… :(

11 2046 03.31.09 at 6:09 pm

find ./* -type f -exec sed -i 's///g' {} \;

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Is Open Solaris gonna defend Linux?

Next post: An introduction to UNIX mail service