search/replace and update the file in perl script

Discussion in 'Perl Misc' started by jrefactors@hotmail.com, Dec 17, 2004.

  1. Guest

    I need a perl script to search and replace the keywords in a file, and
    update that file with new
    keyword.

    The usage should be perl modify.pl oldvalue newvalue filename

    I wrote the script, I can search and replace, but I guess
    the problem is I don't know how to update the file.

    #!/usr/bin/perl
    $oldValue = $ARGV[0];
    $newValue = $ARGV[1];
    $fileName = $ARGV[2];

    open(FILE, $fileName) or die("Unable to open file");

    # read file into an array
    @data = <FILE>;

    foreach $line (@data)
    { print $line;
    $line =~ s/$oldValue/$newValue/
    }


    Please advise. Thanks!!
     
    , Dec 17, 2004
    #1
    1. Advertising

  2. wrote:
    > I need a perl script to search and replace the keywords in a file, and
    > update that file with new
    > keyword.
    >
    > The usage should be perl modify.pl oldvalue newvalue filename
    >
    > I wrote the script, I can search and replace, but I guess
    > the problem is I don't know how to update the file.

    [...]
    > Please advise. Thanks!!


    Please see
    perldoc -q "change one line"

    jue
     
    Jürgen Exner, Dec 17, 2004
    #2
    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. dpackwood
    Replies:
    3
    Views:
    1,865
  2. Schif Schaf
    Replies:
    12
    Views:
    1,316
    Anthra Norell
    Feb 8, 2010
  3. Abby Lee
    Replies:
    5
    Views:
    476
    Abby Lee
    Aug 2, 2004
  4. Michael
    Replies:
    2
    Views:
    90
    Gunnar Hjalmarsson
    Aug 24, 2004
  5. Replies:
    0
    Views:
    106
Loading...

Share This Page