Search for a particular line from a file and paste it in all the other files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • powerfulperl
    New Member
    • Feb 2010
    • 14

    Search for a particular line from a file and paste it in all the other files

    I have several files Log.100 Log.200,Log.300 .... which are in an array
    Log.100 file contains the data
    gpg RC (54321)
    Testion option OK
    Local=IN
    AIX.SHA1.So

    and Log.200 file and other files contains data
    gpg RC (54321)
    Testion option OK
    Linux.SHA1.so
    All files misses the line 'Local=IN' except Log.100. So I want to copy the line Local=IN and then paste it in all the remaining files after the line 'Testing option OK'.

    Note:- When I read the files by an array, I am not sure which file i.e (Log.100, Log.200,or Log.300 I will be reading first)
    All the Log files are in a one single directory.

    My plan was to use grep command in the perl script and traverse each file through an array and whenever it finds that line copy and save it in a variable and again traverse all the files and paste the variable value after the line 'Testion Option OK' in all the files.
    Kindly provide the code.
    your help will be appreciated
    Thanks
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    With what part of this problem are you having difficulty?
    Please be specific.
    Do you know how to get a list of file names?
    Do you know how to open a file?
    Do you know how to test a line of a file for a specific string?

    Show your code, and you will get help on how to improve it.

    Comment

    • powerfulperl
      New Member
      • Feb 2010
      • 14

      #3
      I have this code with me, but I am looking at easy and sophisticated code. Well I want the code to be very understandable and easy.

      Code:
      use strict; 
      use warnings; 
      my @filenames => (dw log.100 log.200 log.300); 
      foreach my $filename (@filenames) { 
          next if /\Afile\.100\Z/xms 
          eval { 
              open my $filehandle, '<', $filename or die Cannot open $filename; 
              open my $temphandle, '>', 'templog' or die Cannot open 'templog'; 
              foreach (<$filehandle>){ 
                  print or die "error copying $filename"; 
                  print 'Local=IN' or die "Error adding line" if /Testing Option OK/; 
              } 
              close $filehandle or die "error closing $filename"; 
              close $temphandle or die "error closing tempfile"; 
              unlink $filename or die "error deleteing original $filename"; 
              rename 'templog', $filename; 
          }; 
      }
      Last edited by numberwhun; Aug 22 '10, 03:12 AM. Reason: Please Use CODE TAGS!

      Comment

      • powerfulperl
        New Member
        • Feb 2010
        • 14

        #4
        Kindly modify the code in a simple way, if possible.

        Comment

        • powerfulperl
          New Member
          • Feb 2010
          • 14

          #5
          I can understand the above code but I want to write it in a different way so that it looks simple.

          Comment

          • toolic
            Recognized Expert New Member
            • Sep 2009
            • 70

            #6
            Your code does not compile. Please fix the syntax errors and post new code inside CODE tags which shows proper indentation.

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              I have added the code tags to your post. In the editor, there is a # symbol. If after you enter your code you highlight it, simply click the hash symbol and it will add the code tags for you.

              Regards,

              Jeff

              Comment

              • powerfulperl
                New Member
                • Feb 2010
                • 14

                #8
                Check if this is ok

                Code:
                   1. use strict; 
                   2. use warnings; 
                   3. my @filenames =(log.100, log.200, log.300); 
                   4. foreach my $filename (@filenames) { 
                   5.     next if /\Afile\.100\Z/xms 
                   6.     eval { 
                   7.         open my $filehandle, '<', $filename; 
                   8.         open my $temphandle, '>', 'templog'; 
                   9.         foreach (<$filehandle>){ 
                  10.             print or die "error copying $filename"; 
                  11.             print 'Local=IN' or die "Error adding line" if /Testing Option OK/; 
                  12.         } 
                  13.         close $filehandle or die "error closing $filename"; 
                  14.         close $temphandle or die "error closing tempfile"; 
                  15.         unlink $filename or die "error deleteing original $filename"; 
                  16.         rename 'templog', $filename; 
                  17.     }; 
                  18. }

                Comment

                • RonB
                  Recognized Expert Contributor
                  • Jun 2009
                  • 589

                  #9
                  This question has been cross posted in multiple forums and the one where this code was given can be found at http://perlguru.com/gforum.cgi?post=...=unread#unread

                  Comment

                  • RonB
                    Recognized Expert Contributor
                    • Jun 2009
                    • 589

                    #10
                    It appears to me that "powerfulpe rl" wants someone to write a custom and fully tested script rather than learning how.

                    My suggestion is to look/ask here http://rentacoder.com/cs/default.aspx

                    Comment

                    • powerfulperl
                      New Member
                      • Feb 2010
                      • 14

                      #11
                      Dude I know what I wanted, I have gone through the code but there seems to be problem with the existing code of mine. I just need a true programmer who can guide me in sorting out my problem rather than wasting time by giving other site links.

                      If you can help then plz assist me.

                      Comment

                      • RonB
                        Recognized Expert Contributor
                        • Jun 2009
                        • 589

                        #12
                        Bill, the person that gave you the code, is a good Perl programmer and I suspect he put in the few extremely minor errors to see if you knew Perl.

                        Lets start with this line, which has 3 errors. Can you find and fix one of them?
                        Code:
                        my @filenames => (dw log.100 log.200 log.300);

                        This line is easier, it has 1 problem. Can you find it?
                        Code:
                        next if /\Afile\.100\Z/xms

                        Comment

                        Working...