Posted By

noah on 07/04/07


Tagged

regex wrapper edit Bash search replace code text commandline files batch hacking perl FileSystem editing productivity tips diamond tricks one-liners


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

icecreamboyy


search and replace across multiple files with Perl


 / Published in: Perl
 

URL: http://perl.com/pub/a/2004/08/09/commandline.html?page=2

A couple of useful snippets from an article I found at Perl.com

Perl search-and-replace on the command line.

All of these should be usable under Cygwin as well. But remember that bash wants single-quoted strings but MS-DOS shell wants strings to be double-quoted.

  1. #print the result of search-and-replace to the terminal
  2. perl -pe 's/bart/milhouse/g' test.html
  3.  
  4. #search-and-replace, with backup
  5. #leave the suffix off of -i to overwrite
  6. perl -i.bak -pe 's/bart/milhouse/g' test.html
  7.  
  8. #echo the number of lines in a file
  9. perl -lne 'END { print $t } @w = /(\w+)/g; $t += @w' test.html
  10.  
  11. #cat file with line numbers
  12. # -p prints $_ each iteration
  13. perl -pe '$_ = "$. = $_"' test.html
  14.  
  15.  
  16. # recursive search-and-replace, only on shells that support file globs
  17. perl -i.bak -pe 's{bart}{milhouse}' **/*html

Report this snippet  

You need to login to post a comment.