DelphiFAQ Home Search:

Search and replace across files using perl

 

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

Question:

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

Answer:

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

Comments:

2006-02-20, 10:50:15
anonymous from United Kingdom  
Hi

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

Thanks
ak
ajay.jindle@gmail.com
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  
Hi
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>

ajay.jindle@gmail.com if you can help
Thanks
aj
2006-03-21, 09:27:32
anonymous from United Kingdom  
rating
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.

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

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

--
Zym
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 ---

--replace.pl---

#!/usr/bin/perl

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

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

while(<INPUT>){
    $_ =~ s/$ARGV[1]/$ARGV[2]/g;
    print $_;
    print OUTPUT $_;
}

close INPUT;
close OUTPUT;



---

call like

perl replace.pl file.html originaltext newtext


----

use at your own risk..
2007-11-26, 22:26:28
anonymous from India  
very useful

 

 

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.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

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.