nixCraft Poll

Topics

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

Posted by Vivek Gite [Last updated: October 27, 2006]

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 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:

Discussion on This Article:

  1. Joe Says:

    To use perl, just remember Perl Pie!

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

  2. LinuxTitli Says:

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

  3. Anonymous Says:

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

  4. Anonymous Says:

    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 Says:

    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 Says:

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

  7. walter Says:

    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 Says:

    thanks for sharing this info

  9. lefty.crupps Says:

    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

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!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.