replace string in file

Discussion in 'Perl Misc' started by wana, Oct 1, 2004.

  1. wana

    wana Guest

    I want to replace a key word in a file with a string. Here is a
    segment of code cut out of a script I wrote to help me generate
    desktop application links.

    <code>
    open TEMPLATE, './template.desktop' or
    die("could not open template.desktop file\n");
    my @template = <TEMPLATE>;
    foreach (@template)
    {
    s/EXEC/$ARGV[0]/g;
    s/NAME/$ARGV[1]/g;
    }
    open OUTFILE, ">$ENV{'HOME'}/Desktop/$ARGV[2]" or
    die("could not open output file\n");
    print OUTFILE @template;
    close OUTFILE;
    </code>

    I was just wondering if there was a better way.


    Thank you to all for help on this question and all previous questions
    I have submitted to this group for which all responses and
    particularly certain special responses have been most helpful.

    wana
     
    wana, Oct 1, 2004
    #1
    1. Advertising

  2. wana wrote:

    > I was just wondering if there was a better way.


    Just an idea: You could use STDIN and STDOUT instead of opening
    filehandles. I don't know if this is a good idea, that belongs to
    your scenario. You could cut it down to (untested):

    #!/usr/bin/perl
    use warnings;
    use strict;
    use diagnostics;

    while(<>) {
    s/EXEC/$ARGV[0]/g;
    s/NAME/$ARGV[1]/g;
    print;
    }

    If you want to use a default file (eg. ./template.desktop) you could
    use this one (again untested):

    [...she-bang and use's...]

    open TPL, '<./template.desktop'
    or die('Unable to open template file.'."\n");
    while(<TPL>) {
    s/EXEC/$ARGV[0]/g;
    s/NAME/$ARGV[1]/g;
    print;
    }
    close TPL;

    You can use this one by running

    $ $SCRIPT exec_replace name_replace > file

    The pro I see here is that you're not doing any buffering of the complete
    content of the input file.

    hth,
    Robert

    --
    http://www.dunkelheit.at/

    »Better to reign in hell than to serve in heaven«
    -- John Milton, »Paradise Lost«
     
    Robert Sedlacek, Oct 1, 2004
    #2
    1. Advertising

  3. wana

    Paul Lalli Guest

    "wana" <> wrote in message
    news:...
    > I want to replace a key word in a file with a string. Here is a
    > segment of code cut out of a script I wrote to help me generate
    > desktop application links.
    >
    > <code>
    > open TEMPLATE, './template.desktop' or
    > die("could not open template.desktop file\n");
    > my @template = <TEMPLATE>;
    > foreach (@template)
    > {
    > s/EXEC/$ARGV[0]/g;
    > s/NAME/$ARGV[1]/g;
    > }
    > open OUTFILE, ">$ENV{'HOME'}/Desktop/$ARGV[2]" or
    > die("could not open output file\n");
    > print OUTFILE @template;
    > close OUTFILE;
    > </code>
    >
    > I was just wondering if there was a better way.
    >


    This is such a common use for perl that there's a couple command line
    arguments available to make this a one-liner:

    perl -pe"s/EXEC/arg0/; s/NAME/arg1/;" ./template.desktop >
    $HOME/Desktop/arg2

    Read about -p and -e in
    perldoc perlrun

    You may also be interested in -i, also in the above perldoc.

    Paul Lalli
     
    Paul Lalli, Oct 1, 2004
    #3
  4. wana wrote:
    > I want to replace a key word in a file with a string. Here is a
    > segment of code cut out of a script I wrote to help me generate
    > desktop application links.
    >
    > <code>
    > open TEMPLATE, './template.desktop' or
    > die("could not open template.desktop file\n");
    > my @template = <TEMPLATE>;
    > foreach (@template)


    There is on point in reading the whole file into an array when afterwards
    you are looping through each line individually anyway. Better process each
    line as you read it:

    while (<TEMPLATE>) {.....

    jue
     
    Jürgen Exner, Oct 1, 2004
    #4
  5. wana

    Joe Smith Guest

    Paul Lalli wrote:

    > This is such a common use for perl that there's a couple command line
    > arguments available to make this a one-liner:
    >
    > perl -pe"s/EXEC/arg0/; s/NAME/arg1/;" ./template.desktop >$HOME/Desktop/arg2


    The /g modifier is still required; "EXEC" could appear multiple times on one
    line as could "NAME".
    -Joe
     
    Joe Smith, Oct 2, 2004
    #5
    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. Umesh
    Replies:
    17
    Views:
    877
  2. Replies:
    1
    Views:
    1,145
    Jesse Houwing
    Sep 17, 2007
  3. Alun
    Replies:
    3
    Views:
    4,521
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    236
    Dr John Stockton
    Aug 27, 2004
  5. V S Rawat
    Replies:
    5
    Views:
    308
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page