469,890 Members | 1,861 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Q: string substitution in a file

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?
Jul 19 '05 #1
6 6086
Troll wrote:
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]?

--
Kind regards, virtual home: http://johnbokma.com/ ICQ: 218175426
web site hints: http://johnbokma.com/websitedesign/
John I count my toes ~ one to ten ~ I meditate ~ and feel the Zen

Jul 19 '05 #2
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
"Troll" <ab***@microsoft.com> wrote in message
news:7l********************@news-server.bigpond.net.au...
| 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?
|
|
Jul 19 '05 #3

"John Bokma" <po********@castleamber.com> wrote in message
news:10***************@halkan.kabelfoon.nl...
Troll wrote:
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]?

--
Kind regards, virtual home: http://johnbokma.com/ ICQ: 218175426
web site hints: http://johnbokma.com/websitedesign/
John I count my toes ~ one to ten ~ I meditate ~ and feel the Zen

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...



Jul 19 '05 #4
*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]?

--
Kind regards, virtual home: http://johnbokma.com/ ICQ: 218175426
web site hints: http://johnbokma.com/websitedesign/
John I count my toes ~ one to ten ~ I meditate ~ and feel the Zen

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/;
Jul 19 '05 #5
"Kaynon McChag" <ka***********@allstream.net> wrote in message
news:9u*******************@news02.bloor.is.net.cab le.rogers.com...
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
"Troll" <ab***@microsoft.com> wrote in message
news:7l********************@news-server.bigpond.net.au...
| 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...


Jul 19 '05 #6
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.
"Troll" <ab***@microsoft.com> wrote in message
news:w7********************@news-server.bigpond.net.au...
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...

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mark | last post: by
reply views Thread by Justin | last post: by
12 posts views Thread by John Leslie | last post: by
18 posts views Thread by james | last post: by
5 posts views Thread by Murali | last post: by
21 posts views Thread by Hitesh | last post: by
27 posts views Thread by user | last post: by
4 posts views Thread by Michael Yanowitz | last post: by
6 posts views Thread by Generic Usenet Account | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.