Programming C# C++ (7) Delphi (604) Java (8) JavaScript (55) perl (40) mysql (3) perl CGI (3) php (12) VBScript (1) Visual Basic (1)
Exchange Links About this site Links to us
|
Search and replace across files using perl
9 comments. Current rating: (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 backupIn this case use the second variation, which tells perl to create a .bak backup file.
 | |  | |
perl -pi -e 's/searchterm/replaceterm/' *.cpp
perl -pi -i.bak -e 's/searchterm/replaceterm/' *.cpp
| |  | |  |
Comments:
|
|
|
|
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
|
|
|
|
|
'Can't open *.html: Invalid argument'
..
means that it did not find any *.html files. Maybe you're in the wrong folder?
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
Found it.
Try perl -pi.bak -e 'BEGIN{@ARGV=<*.html>} s/searchterm/replaceterm/g'
--
Zym
|
|
|
|
|
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?
|
|
|
|
|
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..
|
|
|
|
|
very useful
|
|