Q: string substitution in a file

T

Troll

Hi,

This is what I have:

#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/;
}

open (INFILE, "ARGV[1]") || etc...
while (<INFILE>) {
replace ();
print $_;
}

but the file contents do not get replaced from one to two. Any hints?
 
J

John Bokma

Troll said:
Hi,

This is what I have:

#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/;
}

open (INFILE, "ARGV[1]") || etc...
^^^^ that's not perl
while (<INFILE>) {
replace ();
print $_;
}

but the file contents do not get replaced from one to two. Any hints?

I guess that there is no file ARGV[1]?
 
K

Kaynon McChag

It looked fine to my eye, so I tried it. Your version was dying at line 8
where it was trying to read in the argument ARGV[1]; I replaced your open
command with my own way of doing things:

my $file = shift;
open (INFILE, $file) or die "Couldn't open $file: $!";

...and the script worked fine.

[kmc@spire:~]$ cat infile
I have one monkey
I have one manatee
I wish I had one bonobo

[kmc@spire:~]$ test.pl infile.txt
I have two monkey
I have two manatee
I wish I had two bonobo


| Hi,
|
| This is what I have:
|
| #!/usr/bin/perl -w
| use strict;
|
| sub replace {
| s/one/two/;
| }
|
| open (INFILE, "ARGV[1]") || etc...
| while (<INFILE>) {
| replace ();
| print $_;
| }
|
| but the file contents do not get replaced from one to two. Any hints?
|
|
 
T

Troll

John Bokma said:
Troll said:
Hi,

This is what I have:

#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/;
}

open (INFILE, "ARGV[1]") || etc...
^^^^ that's not perl
while (<INFILE>) {
replace ();
print $_;
}

but the file contents do not get replaced from one to two. Any hints?

I guess that there is no file ARGV[1]?


Sorry John. I could have included more info. The script is run like
$ replace.pl DIR file.txt <<< where DIR is the directory location of the
file and file.txt is the file itself. Both exist.

What I'm trying to do is pass the directory name and the filename as command
line parameters. This file then gets modified ie. one is replaced with two.
Is this clearer?

Sorry there was a typo here. Doesn't this open the file.txt passed thru the
command line?
open (INFILE, "$ARGV[1]") || etc...
 
T

Troll

*snip*
open (INFILE, "ARGV[1]") || etc...
^^^^ that's not perl
while (<INFILE>) {
replace ();
print $_;
}

but the file contents do not get replaced from one to two. Any hints?

I guess that there is no file ARGV[1]?


Sorry John. I could have included more info. The script is run like
$ replace.pl DIR file.txt <<< where DIR is the directory location of the
file and file.txt is the file itself. Both exist.

What I'm trying to do is pass the directory name and the filename as command
line parameters. This file then gets modified ie. one is replaced with two.
Is this clearer?

Sorry there was a typo here. Doesn't this open the file.txt passed thru the
command line?
open (INFILE, "$ARGV[1]") || etc...

Actually there's one more line in the script which I omitted. Sorry:

#!/usr/bin/perl -w
use strict;


chdir $ARGV[0]; # changes to the location of the file

sub replace {
s/one/two/;
 
T

Troll

Kaynon McChag said:
It looked fine to my eye, so I tried it. Your version was dying at line 8
where it was trying to read in the argument ARGV[1]; I replaced your open
command with my own way of doing things:

my $file = shift;
open (INFILE, $file) or die "Couldn't open $file: $!";

..and the script worked fine.

[kmc@spire:~]$ cat infile
I have one monkey
I have one manatee
I wish I had one bonobo

[kmc@spire:~]$ test.pl infile.txt
I have two monkey
I have two manatee
I wish I had two bonobo


| Hi,
|
| This is what I have:
|
| #!/usr/bin/perl -w
| use strict;
|
| sub replace {
| s/one/two/;
| }
|
| open (INFILE, "ARGV[1]") || etc...
| while (<INFILE>) {
| replace ();
| print $_;
| }
|
| but the file contents do not get replaced from one to two. Any hints?
|
|


This is odd. I have exactly this:
#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/g;
}

open (INFILE, "$ARGV[1]") || etc... <<< there was a typo here in the
original post as it had no $
while (<INFILE>) {
replace ();
print $_;
}

I even tried what u suggested Kaynon with no luck:
my $file = shift;
open (INFILE, $file) or die "Couldn't open $file: $!";

My output from print $_ looks like:
two
two
two

but cat test.txt still gives me:
one
one
one

I may need a rest...
 
K

Kris Wempa

I think you are a little confused about what your program is doing. It will
NOT change the original file. It's simply reading the file line by line and
changing the STRING from 'one' to 'two'. If you want the file to change,
you'll have to specifically print the changed lines back to a filehandle.
If your program is printing out a bunch of 'two's, then it is behaving
correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top