Skrevet av Emne: Perl: Simple search and replace in multiple files  (Lest 2640 ganger)

ATC

  • Gjest
Perl: Simple search and replace in multiple files
« på: 22. Oktober 2009, 14:03 pm »
  • [applaud]0
  • [smite]0
  • How to search and replace a string in multiple files?



    ATC

    • Gjest
    [Solved] Perl: Simple search and replace in multiple files
    « Svar #1 på: 22. Oktober 2009, 14:03 pm »
  • [applaud]0
  • [smite]0
  • "perl -pi -w -e 's/search/replace/g;' *.txt"

    'search' is a regular expression. This means you have to escape most special characters with a backslash (\) and you can add lots of advanced stuff like pattern matching, look-ahead, look-behind etc.

    -p loop through parameters (in this case, all files matching*.txt)
    -i edit in-place
    -w enable warnings
    -e execute the quoted script

    Google 'regular expressions' or 'perlretut' to learn more about how to do really cool stuff with regular expressions.