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 ![]()
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Capturing or cutting few characters of a string
- Create function keys based shortcut to speeding up work at Linux command line part # 2
- Linux commands to help you navigate
- Linux text utilities - an overview of text tools for noobs
- nixCraft FAQ Roundup
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!


To use perl, just remember Perl Pie!
perl -p -i -e ’s/hello/goodbye/g’ textfile.txt
Joe, very nice. Your tip sound yummy
thanks for sharing with us
http://www.debian-administration.org/articles/298 has a fine article and discussion on Perl Pie.
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.
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
This does the trick perfectly! Thanks for sharing this special character technique nixcraft!
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!
thanks for sharing this info
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