DelphiFAQ Home Search:

Search and replace across files using perl


comments16 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 1 of 2, other pages: [1] 2
2006-02-20, 10:50:15
anonymous from United Kingdom  

I tried using the above command and all i get is K:\test>perl -pi -e 's/searchterm/replaceterm/' *.html
Can't open *.html: Invalid argument.

can you help

2006-02-20, 12:44:01
anonymous from United States  
'Can't open *.html: Invalid argument'
means that it did not find any *.html files. Maybe you're in the wrong folder?
2006-02-21, 01:27:07
anonymous from United Kingdom  
Hi Thanks but i wish it was as easy as that unfortunatly i was in the right folder. so if you have antother tip or suggestion would be much appericated
2006-02-21, 05:46:59
anonymous from United Kingdom  
The above works fine if i supply a specific file name but not with a *.txt.

hereis what i did
Directory of C:\Documents and Settings\aajind\Desktop\battest

21/02/2006 12:28 <DIR> .
21/02/2006 12:28 <DIR> ..
14/02/2006 10:52 7,957 help.txt
16/02/2006 15:51 346 mynewsam.bat
21/02/2006 12:28 53 sample.txt
21/02/2006 12:28 53 sample.txt.bak
10/02/2006 13:22 33 sample2.txt
14/02/2006 08:23 38 sample3.txt
14/02/2006 10:33 692 testaj.bat
14/02/2006 10:33 692 testaj.vbs
8 File(s) 9,864 bytes
2 Dir(s) 32,098,480,128 bytes free

C:\Documents and Settings\aajind\Desktop\battest>perl -pi -i.bak -e 's/ma/sa/' *.txt
Can't open *.txt: Invalid argument.

C:\Documents and Settings\aajind\Desktop\battest>perl -pi -i.bak -e 's/ma/sa/' sample.txt

C:\Documents and Settings\aajind\Desktop\battest> if you can help
2006-03-21, 09:27:32
anonymous from United Kingdom  
I have the same problem 'Can't open *.html: Invalid argument' . The script works in a directory under Linux, but in the same directory ('folder') under WinXP the script throws up the error message. In my Googling I've come across a suggestion that -i doesn't work, but not a clear explanation of why and what the workarounds are.

2006-03-21, 09:38:10
anonymous from United Kingdom  
Found it.

Try perl -pi.bak -e 'BEGIN{@ARGV=<*.html>} s/searchterm/replaceterm/g'

2007-05-15, 06:46:47
anonymous from India  
Can anyone interested to answer me. I have a html file with some text written on it. Can i search for it and retrive more of the details from that html file?
2007-07-31, 13:41:06
anonymous from United States  

sample perl code ---


if(-e $ARGV[0])
    $cmd = 'copy $ARGV[0] $ARGV[0].bak';
    print 'File does not exist.\n';

open(INPUT,'$ARGV[0].bak') or die 'Cannot open file: $!\n';

    $_ =~ s/$ARGV[1]/$ARGV[2]/g;
    print $_;
    print OUTPUT $_;

close INPUT;
close OUTPUT;


call like

perl file.html originaltext newtext


use at your own risk..
2007-11-26, 22:26:28
anonymous from India  
very useful
2008-07-07, 05:27:04
anonymous from India  
How to modify the given command if my Input String (Finding term) has / ..?
2008-12-23, 03:10:30   (updated: 2008-12-23, 03:48:35)
anonymous from Singapore  
I tried for a set of text files it did not work.But for a single file it will work
Any suggestions??

2009-06-02, 08:29:48
The way that Windows expands file names is different from UNIX, which is why you can't use *.html or *.txt with the in-place search and replace. Just use a command-line for loop:

for %x in (*.html) do perl -pi -i.bak 's/searchterm/replaceterm/' %x

2009-11-26, 22:08:14
anonymous from United States  
cool, i just used this to remove some white spaces in a dhtml menu i copy pasted into vi:

sudo perl -pi -i.bak -e 's/\s{0,}(\<.*)/$1/g' dhtml_menu.dhtml

'just imagine an html markup file with 0 to many amount of whitespace before each tag'

kinda ugly but it works !! =)
2010-02-18, 01:44:02
anonymous from India  
@Tina, basic search and replace script can be used for multiple files as well punching with a shell script.
using for loop of bash script

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
You are on page 1 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: