For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2005 > open > file && s/ / /;









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author open > file && s/ / /;
Brian Volk

2005-07-27, 5:02 pm

Hi all,

Can someone pls take a look at the script below and explain what I'm doing
wrong.. The script runs w/out errors but the substitution is not working..
Hopefully the note below will be enough info..

I was able to print the file names that I wanted but once I tried to open
the file and s / / /; the wheels fell off.. :~)

I think I can open a file this way....? Pls explain what I am doing
wrong....
Thank you!
---------------------------------
#!/usr/local/bin/perl

use strict;
use warnings;

# dir w/ text files containing text and links
my $dir = "J:/flash_host/ecomm/descriptions/product/small";

# a text file that has the file names which contain broken links
my $bad_file = "c:/brian/spartan/bad_links.txt";

open BAD, "< $bad_file" or die "Can't read $bad_file: $!\n";

# name each record in $bad_file => $file
while (my $file = <BAD> ) {
chomp $file;

if (-e "$dir/$file") {

# open the text file w/ a bad link and sub "http://..." w/ a local
file
open BADFILE, "> $dir/file" or die "Can't open $dir/$file for replace:
$!\n" &&
s & http://.* & descriptions/product/MSDS/$file &;
close BF
}

}
-----------------------------------

Brian Volk
HP Products
317.298.9950 x1245
<mailto:bvolk@hpproducts.com> bvolk@hpproducts.com


John W. Krahn

2005-07-27, 5:02 pm

Brian Volk wrote:
> Hi all,


Hello,

> Can someone pls take a look at the script below and explain what I'm doing
> wrong.. The script runs w/out errors but the substitution is not working..
> Hopefully the note below will be enough info..
>
> I was able to print the file names that I wanted but once I tried to open
> the file and s / / /; the wheels fell off.. :~)
>
> I think I can open a file this way....? Pls explain what I am doing
> wrong....
> Thank you!
> ---------------------------------
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> # dir w/ text files containing text and links
> my $dir = "J:/flash_host/ecomm/descriptions/product/small";
>
> # a text file that has the file names which contain broken links
> my $bad_file = "c:/brian/spartan/bad_links.txt";
>
> open BAD, "< $bad_file" or die "Can't read $bad_file: $!\n";
>
> # name each record in $bad_file => $file
> while (my $file = <BAD> ) {
> chomp $file;
>
> if (-e "$dir/$file") {
>
> # open the text file w/ a bad link and sub "http://..." w/ a local
> file
> open BADFILE, "> $dir/file" or die "Can't open $dir/$file for replace:
> $!\n" &&
> s & http://.* & descriptions/product/MSDS/$file &;
> close BF
> }
>
> }
> -----------------------------------


Perl provides some shortcuts to allow you to do what you want:

#!/usr/local/bin/perl

use strict;
use warnings;

# dir w/ text files containing text and links
my $dir = 'J:/flash_host/ecomm/descriptions/product/small';

# a text file that has the file names which contain broken links
my $bad_file = 'c:/brian/spartan/bad_links.txt';

open BAD, '<', $bad_file or die "Can't read $bad_file: $!\n";

# store the files to "edit" in @ARGV
@ARGV = map { chomp; "$dir/$_" } <BAD>;

# set the in-place edit variable
# cannot be '' on Windows
$^I = '.bak';

# modify the files and save the originals with .bak extention
while ( <> ) {
s & http://.* & descriptions/product/MSDS/$file &;
print;
}

__END__



John
--
use Perl;
program
fulfillment
Brian Volk

2005-07-28, 9:59 pm



> -----Original Message-----
> From: John W. Krahn [mailto:krahnj@telus.net]
> Sent: Wednesday, July 27, 2005 3:47 PM
> To: Perl Beginners
> Subject: Re: open > file && s/ / /;
>
>
> Brian Volk wrote:
>
> Hello,
>
> what I'm doing
> is not working..
> tried to open
> "http://..." w/ a local
> $dir/$file for replace:
>
> Perl provides some shortcuts to allow you to do what you want:
>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> # dir w/ text files containing text and links
> my $dir = 'J:/flash_host/ecomm/descriptions/product/small';
>
> # a text file that has the file names which contain broken links
> my $bad_file = 'c:/brian/spartan/bad_links.txt';
>
> open BAD, '<', $bad_file or die "Can't read $bad_file: $!\n";
>
> # store the files to "edit" in @ARGV
> @ARGV = map { chomp; "$dir/$_" } <BAD>;
>
> # set the in-place edit variable
> # cannot be '' on Windows
> $^I = '.bak';
>
> # modify the files and save the originals with .bak extention
> while ( <> ) {
> s & http://.* & descriptions/product/MSDS/$file &;
> print;
> }
>
> __END__
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>


John.. Thank you for the reply! Just a couple of things I don't
understand...

1.) > # set the in-place edit variable
> # cannot be '' on Windows
> $^I = '.bak';


This worked great on WinXP but when I went home and tried it on Linux it did
not work. Which is what, I'm sure your comments ment.. :~) How would I
write $^I = '.bak'; on Linux?

2.) The subsitution didn't work on some of the files... could this have to
do w/ CR or LF? Here is an example:

----- text file revised ----
NAD-75 - Non-ammoniated formula for removal of water-based waxes and polymer
floor finishes. ~

http://www.spartanchemical.com/sfa/...da8525665f00619
f51/ 03578511e75fcee485256fb1005c3e3f!OpenDoc
ument

----- text file .bak ----
NAD-75 - Non-ammoniated formula for removal of water-based waxes and polymer
floor finishes. ~

http://www.spartanchemical.com/sfa/...da8525665f00619
f51/ 03578511e75fcee485256fb1005c3e3f!OpenDoc
ument

In this file you can see that it worked great...

---- text file revised ----
No-rinse disinfectant detergent.Use only with Lean Clean On The Go
Dispenser. EPA Reg. No. 1839-167-5741. ~
descriptions/product/MSDS/

----- text file .bak
No-rinse disinfectant detergent.Use only with Lean Clean On The Go
Dispenser. EPA Reg. No. 1839-167-5741. ~
http://www.spartanchemical.com/sfa/...da8525665f00619
f51/ 02d57310a7f9d2c185256def006df04d!OpenDoc
ument

Any ideas pls let me know.... Thanks again for your help!

Brian Volk
John W. Krahn

2005-07-28, 9:59 pm

Brian Volk wrote:
>
>
> John.. Thank you for the reply! Just a couple of things I don't
> understand...
>
> 1.) > # set the in-place edit variable
> > # cannot be '' on Windows
> > $^I = '.bak';
>
> This worked great on WinXP but when I went home and tried it on Linux it did
> not work. Which is what, I'm sure your comments ment.. :~) How would I
> write $^I = '.bak'; on Linux?


I have used it many times on Linux and it has worked for me. What does
"it did not work" mean exactly?


> 2.) The subsitution didn't work on some of the files... could this have to
> do w/ CR or LF?


It could be anything, it is hard to tell without seeing the actual
program and data.



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com