Ano-Tech Computers
Enter keyword:

Perl: Simple search and replace in multiple files
Problem:
How to search and replace a string in multiple files?
 
Solution:
"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.
 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles