Q: string substitution in a file

Discussion in 'Perl' started by Troll, Sep 24, 2003.

  1. Troll

    Troll Guest

    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?
     
    Troll, Sep 24, 2003
    #1
    1. Advertising

  2. Troll

    John Bokma Guest

    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
     
    John Bokma, Sep 24, 2003
    #2
    1. Advertising

  3. Re: string substitution in a file

    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?
    |
    |
     
    Kaynon McChag, Sep 24, 2003
    #3
  4. Troll

    Troll Guest

    "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...
     
    Troll, Sep 24, 2003
    #4
  5. Troll

    Troll Guest

    *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/;
     
    Troll, Sep 24, 2003
    #5
  6. Troll

    Troll Guest

    Re: string substitution in a file

    "Kaynon McChag" <> wrote in message
    news:9u8cb.73712$...
    > 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...
     
    Troll, Sep 24, 2003
    #6
  7. Troll

    Kris Wempa Guest

    Re: string substitution in a file

    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...
    >
    >
    >
    >
     
    Kris Wempa, Sep 26, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. peter leonard
    Replies:
    0
    Views:
    312
    peter leonard
    Sep 22, 2003
  2. sdd
    Replies:
    1
    Views:
    769
    Diez B. Roggisch
    Nov 3, 2003
  3. Ondrej Krajicek

    Keyword substitution in string

    Ondrej Krajicek, Aug 12, 2004, in forum: Python
    Replies:
    3
    Views:
    548
    Simon Dahlbacka
    Aug 12, 2004
  4. Murali

    Python String Substitution

    Murali, Jan 26, 2006, in forum: Python
    Replies:
    5
    Views:
    13,459
    Steven D'Aprano
    Jan 28, 2006
  5. John Salerno
    Replies:
    16
    Views:
    459
    bruno at modulix
    May 12, 2006
Loading...

Share This Page