Home > Linux > Search and replace in multiple files

Search and replace in multiple files

February 25th, 2009 deevis

If you ever need to search and replace strings throughout a bunch of files here’s a neat, somewhat alternative, way to do it.

for file in `ls **/*.txt`;do perl -pi -w -e ’s/SEARCH_FOR_THIS/REPLACE_WITH_THIS/g;’ $file; done

The nested ‘perl’ command can work kind of like ’sed’.  Here are what the arguments being passed in mean:

-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • TwitThis
Categories: Linux Tags: ,
Comments are closed.