Search and replace across multiple files

/ Published in: Perl
Save to your folder(s)

-p tells perl to assume a simple loop around the code we give it to run.

-i.bak tells perl that we wish to create backups of the files we modify with the suffix .bak.

-e "..." tells perl that we wish to execute the code inside the quotes.

And finally the file list is the list of files that we wish to operate upon.

Escape special characters such as ".", "?", "*", or "/" by preceding them with the "\" character.

Example: change "Bob" to "Steve" on all HTML files in the current directory:

perl -pi.bak -e "s/Bob/Steve/g" *.html

Copy this code and paste it in your HTML
  1. perl -pi.bak -e "s/Old text/New text/g" file1 file2 file3


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.