Awk/Sed: How to do a recursive find/replace of a string?

3 points

I am trying to replace specific text within several files (maybe around 200 files). Is there a way that I could this without having to replace the text manually.

I am actually trying to change the IPs in several httpd.conf, eg. 192.168.0.5 to 192.168.0.10. I have googled and some of the sites suggested using grep and awk, but no specific help was found by me.

Any help is greatly appreciated. Thanks.

Created by mamaykina 45 weeks 5 days ago
  Tags:

Answer(s):

0 points

You can do

Code:
sed 's/original_string/new_string/' file1 file2 file3 ...

If the strings contain the character / you can use any other character
for delimiter.

On second thought this will send all the output to the screen.
So I guess you should do

Code:
sed 's/original_string/new_string/' file > tmp_file
mv tmp_file file

and repeat this for every file you want changed.

Created by ranipoojax
1 point

You can use Perl and Find as following:

perl -p -i -e 's/oldstring/newstring/g' `find ./ -name *.html`

Created by Anonymous

Post your Answer

  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Links to specified hosts will have a rel="nofollow" added to them.

More information about formatting options