David Wall wrote:
>
>
> --On Saturday, September 06, 2003 7:45 PM -0400 perlwannabe
> <[EMAIL PROTECTED]> wrote:
>
>> OK, I have a new problem. I need to delete an entire line that contains
>> certain text. I have done an extensive search and had no luck finding an
>> adequate answer. Yes, I also saw the FAQ that just refers me to
>> Tie::file. The most promising solution was:
>>
>> perl -ni.bak -e 'print unless /FOO/;' input.txt
>>
>> but when I run that line from the c:\ I get the following error:
>>
>> Can't find string terminator "'" anywhere before EOF at -e line 1.
>
> Windows is weird that way. Use double-quotes instead of single-quotes.
>
>
>> Also, I would like to run this from within a script instead of from the
>> command line.
>>
>> BTW...using PERL 5.8.0 on Windows2000 Pro
>
> Take a look at 'perldoc perlrun' -- it explains how all the command-line
> options work and give equivalent code.
Perlfaq5 also has a nice tidbit on how to do this while within a script.
I used this trick recently to do some updates to an existing website where I
needed to move a bunch of images from the main html/ directory to a
subfolder html/images/ *provided* that the images were actually used in the
existing html files; otherwise they should be skipped and left in the dir
so I can move them elsewhere manually.
here's the code I wound up using. Hopefully you'll find this a useful
example. I've commented the code to make things more obvious.
#!/usr/bin/perl
use warnings;
use strict;
$|++;
use File::Glob;
# make these operations fatal if they fail
use Fatal qw( chdir open close rename );
# go here, and scan for jpg images that don't belong here.
chdir "/home/webdragon/webclient/bigbronze/html";
my @jpegfiles = glob( q{*.jpg} );
foreach my $jpegfile (sort @jpegfiles)
{
# find every file that contains the image as a src="filename.jpg"
chomp(my @htmlfiles = qx{grep -l 'src="$jpegfile"' *});
# skip it if we couldn't find any html files referencing this image.
unless (@htmlfiles)
{
print "$jpegfile was not found in any other file\n\n";
next;
}
# tell us about it
print "$jpegfile was found in: @htmlfiles\n";
# quote the expression for the document replacement so we don't
# have to do this multiple times
my $regex = qr{\Qsrc="$jpegfile"};
# wrap the local magic in a block to keep the scoping sensible
{
# set a local -i.bak and file list
# just as if we passed this on a command line to a
# (perl -pi -e '#code here' filename filename2 filename3) one-liner
# this deep magic lets us edit the files in-place from within this script.
local ($^I, @ARGV) = ('', @htmlfiles);
# more magic with @ARGV
while (<>)
{
# fix the newline for foreign files as long as we're here
s#\015\012|\015|\012#\n#;
# fix our image location to the subdirectory where we really want them
s#$regex#src="images/$jpegfile"#;
print;
# resets $. using yet more ARGV filehandle magic. nifty trick, this.
close ARGV if eof;
}
print " -- edited @htmlfiles\n";
}
# move the image too!
rename "$jpegfile", "images/$jpegfile";
print " -- moved $jpegfile to images/\n\n";
}
__END__
now, you'll note that here I used -i rather than -i.bak since with 900 of
these images referencing 250 html files, you wind up with
blah.htm.bak.bak.bak.bak or blah.htm.~.~.~.~.~.~ and swiftly hit a filename
size limitation in your OS after a very short bit. so I used a few 'last'
statements to test it on ONE file, and then removed the backup stuff and
had it run in-place with $^I set to '' instead.
HTH!
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]