Loading ...
Sorry, an error occurred while loading the content.

Replacement of one line by many lines..

Expand Messages
  • Sreeram B S
    HI friends, I have a peculiar query.. Can I replace a single line by multiple-lines using any of perl features..? Suppose there is a perl file which runs
    Message 1 of 2 , Nov 6, 2003
    • 0 Attachment
      HI friends,

      I have a peculiar query..
      Can I replace a single line by multiple-lines using any of perl features..? Suppose there is a perl file which runs to,say 1000 lines, and there are many such files.Now I would like to add the 'author and the usage' in the second line for future consultation in each file. I would like to add these lines in the 2nd line of the program because the first line is #!/usr/bin/perl.
      How can I do this..?

      I spent quite a considerable time on finding out a solution to this problem. I would like to just paste the commands which I issued..(all these failed)...

      (i) perl -pi -e '2s/^.*$/`cat message`/g' file1.pl - whereis message is the file containing the author name and the usage part.
      (ii) sed '2s/^.*$/`cat message`/' file1.pl - The second line was replaced by `cat message` itself but not the actual message.
      ... and many such commands(combination). But I used only perl -pi -e .. and sed...

      Kindly help.

      Thanks,
      Sreeram



      ---------------------------------
      Do you Yahoo!?
      Protect your identity with Yahoo! Mail AddressGuard

      [Non-text portions of this message have been removed]
    • J.E. Cripps
      ... umm sed isn t Perl now, is it? Some ppl here won t know what sed is alas. if you _had_ a sed solution though you might use s2p to get a perl one...
      Message 2 of 2 , Nov 6, 2003
      • 0 Attachment
        Sreeram lately queried:
        > HI friends,
        >
        > I have a peculiar query..
        > Can I replace a single line by multiple-lines using any of
        > perl features..? Suppose there is a perl file which runs to,say 1000
        > lines, and there are many such files.Now I would like to add the
        > author and the usage' in the second line for future consultation
        > in each file. I would like to add these lines in the 2nd line of the
        > program because the first line is #!/usr/bin/perl.
        > How can I do this..?

        > (i) [I snipped out this folks, sorry. it was some perl 1 liner]
        > (ii) sed '2s/^.*$/`cat message`/' file1.pl -

        umm "sed" isn't Perl now, is it? Some ppl here won't know
        what sed is alas. if you _had_ a sed solution though
        you might use s2p to get a perl one...

        you might start with this:
        =======================================
        #!/usr/bin/perl -w

        use strict;

        my $the_added_line = "#whatever it is\n";
        # or store yr addition in a file, open that, etc.
        # assign yr $the_added_line from that

        while (<>) {
        if ( /^#\!\/usr\/bin\/perl/) {
        print;
        print "$the_added_line";}
        else {
        print;}
        }
        ========================================================
        the while (<>) loop of course is only good for one file
        find out how to get a directory list and loop over that
      Your message has been successfully submitted and would be delivered to recipients shortly.