replace multiple lines in a file with Perl

Discussion in 'Perl Misc' started by learner, Dec 16, 2004.

  1. learner

    learner Guest

    Hi, all,
    I 've searched high and low, just couldn't find a single decent
    example to replace multiple lines from a file.

    So I did this:

    #!/usr/local/bin/perl
    undef $/;
    $original = "
    // This is an old text
    // that is across several
    // lines in this file. This file
    // also contains the copy right
    // symbol \251.";
    $replacement = "
    // I just want to replace
    // the old lines in one fell
    // swoop!";
    $sHOME = "./changed/";
    foreach $file (@ARGV) {
    if (! open(INPUT,"<$file") ) {
    print "Can't open input file $file\n";
    next;
    }
    $data=<INPUT>;
    close INPUT;

    if ($data =~ s/$original/$replacement/g) {
    $newfile = $sHOME.$file;
    print $newfile."\n";
    if (! open(OUTPUT,">$newfile") ) {
    die "Can't open output file $newfile\n";
    }
    print OUTPUT $data;
    close OUTPUT;
    print "$file changed\n";
    }
    else { print "$file not changed\n"; }
    }
    exit(0);

    Then I would do:
    perl thisperlfile.pl name.of.file.with.original.txt

    The input file contains only one copy of the original text, but it
    created more than 50 replacement txt in the final file. I am really
    puzzled. Could someone help please? Thanks.
     
    learner, Dec 16, 2004
    #1
    1. Advertising

  2. learner wrote:

    > s/$original/$replacement/g


    Sorry, too early to read it completely, but you may find help in the docs
    for regular expressions. Look out for the modifiers (here you just have g).
    Maybe you should also look at qr//.

    g,
    Robert

    --
    http://www.dunkelheit.at/
    I want, therefore I can.
     
    Robert Sedlacek, Dec 16, 2004
    #2
    1. Advertising

  3. "learner" <> writes:
    > I 've searched high and low, just couldn't find a single decent
    > example to replace multiple lines from a file.
    >
    > [...]
    >
    > The input file contains only one copy of the original text, but it
    > created more than 50 replacement txt in the final file. I am really
    > puzzled. Could someone help please? Thanks.


    I ran your program, but it seemed to do what it should with my simple
    input file. Exactly what does your input look like?
     
    Arndt Jonasson, Dec 16, 2004
    #3
  4. learner <> wrote:

    > Could someone help please? Thanks.



    Yes, if you provide a short and complete program *that we can run*
    that illustrates your problem.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 16, 2004
    #4
  5. learner

    learner Guest

    Well, did you read my first post, I did include a 'short and complete
    program *that you can run*'.
     
    learner, Dec 16, 2004
    #5
  6. learner

    learner Guest

    OK, here is what I actually need to do. we have this big program
    project that has many source code files, and I want to replace the old
    copyright notice that is included in many files with a new one.

    The old one looks something like this:

    // MyBigBuckSoft
    //
    // Laboratory for Applications of Land Surveying
    // XXXYYY PPPPPP
    // West xxxxyyyyy, XX 55555
    // Copyright (1988-2004)
    // © Xxxyyy RKKKKK Foundation
    // All rights reserved.

    Then I will need to replace it with a new one across multiline. If you
    could, please try to replace this old txt to see if you get the same
    kind of weird result as I did. I am not sure if that 'copyright' symbol
    is the reason?!
     
    learner, Dec 16, 2004
    #6
  7. learner

    Paul Lalli Guest

    "learner" <> wrote in message
    news:...
    > Well, did you read my first post, I did include a 'short and complete
    > program *that you can run*'.


    Oh, okay, let me go run that file....

    Can't open output file name.of.file.with.original.txt

    Gee. Doesn't work.

    Posting a file that can be run, when dependent on external data,
    requires the use of the __DATA__ marker. We don't have any sample input
    data with which to run your program.

    Please (re-)read the Posting guidelines, paying attention to the section
    labeled "Provide enough information"

    Paul Lalli
     
    Paul Lalli, Dec 16, 2004
    #7
  8. learner

    learner Guest

    Ok, Please do these steps to run the program I posted:
    1. copy and paste the perl program into an .pl file, say it is 'rep.pl'
    2. in the same directory, create a new directory called 'changed'
    3. create a new file with whatever name you want, say it is called
    'oldtxt', which contains the oldtxt that you like to replace (the same
    txt listed in the program)
    4. then you issue command :

    perl rep.pl oldtxt
    to run it.
    Hope this will help, and see if you can help me out in return. Thanks.
     
    learner, Dec 16, 2004
    #8
  9. learner wrote:
    > OK, here is what I actually need to do. we have this big program
    > project that has many source code files, and I want to replace the old
    > copyright notice that is included in many files with a new one.
    >
    > The old one looks something like this:
    >
    > // MyBigBuckSoft
    > //
    > // Laboratory for Applications of Land Surveying
    > // XXXYYY PPPPPP
    > // West xxxxyyyyy, XX 55555
    > // Copyright (1988-2004)
    > // © Xxxyyy RKKKKK Foundation
    > // All rights reserved.
    >
    > Then I will need to replace it with a new one across multiline. If you
    > could, please try to replace this old txt to see if you get the same
    > kind of weird result as I did. I am not sure if that 'copyright' symbol
    > is the reason?!


    No, the copyright character is not special in regular expression
    patterns, but parentheses are, so they need to be escaped. You can do
    this when assigning the $original variable:

    Copyright \\(1988-2004\\)

    but the easiest way to deal with it is to use \Q:

    s/\Q$original/$replacement/g

    Please see "perldoc perlre".

    HTH

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 16, 2004
    #9
  10. learner wrote:

    > Ok, Please do these steps to run the program I posted:
    > 1. copy and paste the perl program into an .pl file, say it is 'rep.pl'
    > 2. in the same directory, create a new directory called 'changed'
    > 3. create a new file with whatever name you want, say it is called
    > 'oldtxt', which contains the oldtxt that you like to replace (the same
    > txt listed in the program)
    > 4. then you issue command :


    There's only one of you, there are many people who may want to see if
    they help you. As such it is more efficient for you (one person) do
    take the steps necessary to produce a mimimal but complete
    (self-contained) test case rather than for everyone else to have to take
    these steps.
     
    Brian McCauley, Dec 16, 2004
    #10
  11. learner

    Paul Lalli Guest

    "learner" <> wrote in message
    news:...

    > Ok, Please do these steps to run the program I posted:


    Explain to me why you believe I (or any other potential reader of this
    group) should have to follow *your* instructions? YOU are the one
    asking for help. WE are the ones generously donating our time and
    effort for the chance of helping YOU. That means you do things the way
    WE ask you to. And *that* means following the Posting Guidelines.

    Paul Lalli
     
    Paul Lalli, Dec 16, 2004
    #11
  12. learner <> wrote:

    > Well, did you read my first post,



    Yes I did, and this is the *last* post of yours that I will be reading.

    Good luck!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 17, 2004
    #12
  13. learner <> wrote:

    > Ok,



    "OK" implies agreement, yet you are not agreeing to post a short
    and complete program that we can run...


    > Please do these steps to run the program I posted:



    Send me a check *before* you start issuing instructions to me.

    I'm busy.


    > see if you can help me out in return.

    ^^^^^^^^^

    In return for *what*?

    You haven't done anything for me, you haven't even done what
    I asked of you.


    If you make it easy for people to answer your question you will
    have a much better chance of getting an answer to your question.

    If you don't, you won't.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 17, 2004
    #13
  14. "learner" <> writes:
    > Ok, Please do these steps to run the program I posted:
    > 1. copy and paste the perl program into an .pl file, say it is 'rep.pl'
    > 2. in the same directory, create a new directory called 'changed'
    > 3. create a new file with whatever name you want, say it is called
    > 'oldtxt', which contains the oldtxt that you like to replace (the same
    > txt listed in the program)
    > 4. then you issue command :
    >
    > perl rep.pl oldtxt
    > to run it.
    > Hope this will help, and see if you can help me out in return. Thanks.


    You were already told to please use the __DATA__ marker to exhibit
    a complete program that fails, including data. It's good advice.
    The comments you're getting are not because it's so very hard for anyone
    to fill in missing details in your problem description, but because it
    should be you doing that work, and besides, those who tried that approach
    couldn't reproduce the problem.
     
    Arndt Jonasson, Dec 17, 2004
    #14
  15. learner

    learner Guest

    Well, everyone,
    I thought the little program I posted is such an simple endeavor
    that it would be really obvious for gurus to disect... and then the
    "instruction" i gave, which invoke so many ANGRY people. Appologize to
    you all!
    Peace.
     
    learner, Dec 17, 2004
    #15
  16. "learner" <> wrote in news:1103302665.844892.234810
    @f14g2000cwb.googlegroups.com:

    > Well, everyone,
    > I thought the little program I posted is such an simple endeavor
    > that it would be really obvious for gurus to disect... and then the
    > "instruction" i gave, which invoke so many ANGRY people.


    Because your instructions presumed that the two minutes it would have taken
    you to do it right was more valuable than the 30 or so minutes 15 different
    people would have spent following your instructions. That is very rude.

    > Appologize to you all!


    If the apology is sincere, then there might still be hope for you. You can
    show that you are sincere by following the posting guidelines in your
    future posts.

    Sinan.
     
    A. Sinan Unur, Dec 17, 2004
    #16
    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. Joe Wright
    Replies:
    0
    Views:
    554
    Joe Wright
    Jul 27, 2003
  2. Murali
    Replies:
    2
    Views:
    610
    Jerry Coffin
    Mar 9, 2006
  3. Johan Martinez
    Replies:
    3
    Views:
    302
    Johan Martinez
    May 22, 2011
  4. Markus Dehmann
    Replies:
    1
    Views:
    150
    Tad McClellan
    Sep 26, 2006
  5. Cah Sableng
    Replies:
    0
    Views:
    263
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page