|
#1
|
|||
|
|||
|
Many people talk about Perl and many more about regular expressions but unless you are a programmer you probably never use either. We will discuss a few unique and very useful ways to use both of them. Have you ever needed to parse multiple files to remove or modify a certain string? Have you ever needed to parse multiple files in subdirectories to change content in them? If so then this tutorial will certainly give you the insight you need. Read Useful Perl Scripts With Regular Expressions tutorial here
__________________
md |
|
#2
|
|||
|
|||
|
How would I implement your script you've shown under "Replace on Multiple files" to limit itself only to the current directory?
Question submitted by e-mail.
__________________
md |
|
#3
|
|||
|
|||
|
The following is code to parse a single directory. You will notice the only thing that changes is the if statement where we add in $File::Find:dir and compare it to our intial directory which is stored in $directory.
On a side note: You will want to make sure that the $directory value DOES NOT have a trailing slash. If it does then you will not get the results you want because $File:Find:dir returns directories without a trailing slash; for example a dir would be /tmp not /tmp/ PHP Code:
__________________
md |
|
#4
|
|||
|
|||
|
This question came from an e-mail.
Thank you. I'll put that to use! To change this so that the directory to parse is handled dynamically, you would change my directory = "/home/directory"; to my directory = $_[0]; correct? I'm no Perl expert but with your help in cases such as this one, maybe I can someday approach the level of expert.
__________________
md |
|
#5
|
|||
|
|||
|
Actually the way you would do it is to use $ARGV[0]
So the code would look like my $directory = $ARGV[0]; instead of my $directory = "/home/directory"; By doing this you would be able to run your perl script from the command line as follows > FindAndReplace.pl /my/directory I hope this helps!
__________________
md |
|
#6
|
|||
|
|||
|
Hi, thank you for writing and posting your script. I found this thread via google.
Anyway, I thought I would contribute my version I use. It recurses, but there is a line commented out to only process a single directory as that is sometimes required. I also like the script to report what it is changing, this can be useful if the output is being written to a file via `>>log` or something. Code:
#!/usr/local/bin/perl
#
# find and replace within HTML files
#
use File::Find;
use strict;
# edit these values for search and replace
my $old = '../images';
my $new = '../../images';
my $directory = $ARGV[0];
die "\nRequires argument for search path\n" unless defined $directory ;
find (\&process, $directory);
sub process
{
my @outLines; #Data we are going to output
my $line; #Data we are reading line by line
# print "processing $_ / $File::Find::name\n";
# Only parse files that end in .html
# if ( ($File::Find:dir eq $directory ) && ($File::Find::name =~ /.html$/ ) ) { # only search one dir
if ( $File::Find::name =~ /\.html$/ ) { # search sub dirs too
open (FILE, $File::Find::name ) or
die "Cannot open file: $!";
print "\n" . $File::Find::name . "\n";
while ( $line = <FILE> ) {
$line =~ s/$old/$new/i;
push(@outLines, $line);
}
close FILE;
open ( OUTFILE, ">$File::Find::name" ) or
die "Cannot open file: $!";
print ( OUTFILE @outLines );
close ( OUTFILE );
undef( @outLines );
}
}
print "\n\n Done changing $old to $new in\n $directory\n";
Please let me know if you see a bug! Thanks for sharing. |
|
#7
|
|||
|
|||
|
write-only
Actually that is a great idea to have variables for what you want changed as it makes it much easier to modify the script. Great job!
__________________
md |
|
#8
|
|||
|
|||
|
I found a bug!
Code:
# if ( ($File::Find:dir eq $directory ) && ($File::Find::name =~ /.html$/ ) ) { # only search one dir
should be: Code:
# if ( ($File::Find::dir eq $directory ) && ($File::Find::name =~ /.html$/ ) ) { # only search one dir
Sometime when making chanes to web sites, you do not want to recurse into subdirectories. perl -c SCRIPT.pl is our friend! |
|
#9
|
|||
|
|||
|
hi i was just wondering
when opening a file in windows the writer of "Useful Perl scripts with Regular Expressions" says you must u must give the windows path "C:\my documents\file.txt". then goes on to say --while it might seem funny to see the double slashes (but there are no double slashes ) wat is da story ?? Thanks mazad |
|
#10
|
|||
|
|||
|
It seems the content management system and display system we use escapes the double slashes.
So it should be either "C:\\directory\\file" or you could also do (I think don't quote me on this "C:/directory/file". I will try to get the site updated as soon as possible.
__________________
md |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|