Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Q: string substitution in a file

Reply
Thread Tools

Q: string substitution in a file

 
 
Troll
Guest
Posts: n/a
 
      09-24-2003
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?


 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      09-24-2003
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

 
Reply With Quote
 
 
 
 
Kaynon McChag
Guest
Posts: n/a
 
      09-24-2003
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" <> wrote in message
news:7l7cb.119993$...
| 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?
|
|


 
Reply With Quote
 
Troll
Guest
Posts: n/a
 
      09-24-2003

"John Bokma" <> wrote in message
news:...
> 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...











 
Reply With Quote
 
Troll
Guest
Posts: n/a
 
      09-24-2003
*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/;


 
Reply With Quote
 
Troll
Guest
Posts: n/a
 
      09-24-2003
"Kaynon McChag" <> wrote in message
news:9u8cb.73712$ 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" <> wrote in message
> news:7l7cb.119993$...
> | 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...




 
Reply With Quote
 
Kris Wempa
Guest
Posts: n/a
 
      09-26-2003
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" <> wrote in message
news:w79cb.120138$...
> 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...
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
which is better, string concatentation or substitution? John Salerno Python 16 05-12-2006 05:53 PM
Python String Substitution Murali Python 5 01-28-2006 12:22 AM
Keyword substitution in string Ondrej Krajicek Python 3 08-12-2004 06:32 PM
String substitution -- reactions? sdd Python 1 11-03-2003 11:08 PM
Begineer Question : Global string substitution with re peter leonard Python 0 09-22-2003 09:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57