DelphiFAQ Home Search:

Search and replace across files using perl


comments17 comments. Current rating: 4 stars (3 votes). Leave comments and/ or rate it.


How can I use perl's powerful string functions to do a search and replace across all files in a directory?


Use perl from the command line as shown below.

On an MSDOS/ Windows system you may get the error message

Can't do inplace edit without backup
In this case use the second variation, which tells perl to create a .bak backup file.

// at the shell command line
// search and replace in all C++ files

perl -pi -e 's/searchterm/replaceterm/' *.cpp

perl -pi -i.bak -e 's/searchterm/replaceterm/' *.cpp

Content-type: text/html


You are on page 2 of 2, other pages: 1 [2]
2010-06-01, 03:53:32
anonymous from United States  
I type the command as below but it seems doesn't work.
The text in the file remain the same as before.

perl -p -i.bak -e 's/\N//' lzy.tsv
2013-10-16, 03:20:17
anonymous from India  
HI all ,

Below code is working only for single . if i want to change multipul file in DIR its not working .can u please suggest how can we write in commond line

perl -pi -i.bak -e 's/searchterm/replaceterm/' *.txt

You are on page 2 of 2, other pages: 1 [2]



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: