Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    86
    Rep Power
    3

    Printing next two lines after a match to a file


    Hi,
    I知 trying to print next two lines after a match to a file.
    But it is more complicated than I thought.
    I cannot redirect the 菟rint" to my file.
    Would you guys check my code?
    Thanks, testerV

    Code:
    #!/usr/local/bin/perl
     use strict;
     use warnings ;
    
    
      my $mtch1 = 'Line1' ;
      my $found ;
      
      my $out = 'C:/MLines.txt' ; 
          open my $out_fh,'>', $out or die "Can't open $out $!\n" ;
             
      my $inf = 'C:/F151.txt' ;  
          open my $inf_fh,'<',$inf or die "Can't open $inf $!\n" ; 
          
           while ( my $line = <$inf_fh> ) {
                      chomp $line ;
                      
                 if ( $line =~ /^$mtch1/ ) {
                   print $line;
                   
                   print scalar <$inf_fh> for 1 .. 2 ;
                   
                 found = scalar <$inf_fh> for 1 .. 2 ;
                          print $out_fh $found, "\n" ;
                 }
               }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    I believe this is one way to do it:
    Code:
    my $mtch1   =  'Line1';
    my $out     =  'C:/MLines.txt';
    my $inf     =  'C:/F151.txt';
    my $line;
    my $count   =  0;
    
    open ( my $out_fh, '>', $out ) or die "Can't open $out $!\n";
    open ( my $inf_fh, '<', $inf ) or die "Can't open $inf $!\n";
    
    while ( $line = <$inf_fh> ) {
      if ( $line =~ /^$mtch1/ or $count > 0 ) {
        $count++;
      }
      if ( $count > 1 and $count <= 3 ) {
        print $line;
        print $out_fh $line;
      }
      # Reset so as to print again if match found again
      if ( $count == 3 ) {
        $count  =  0;
      }
    }
    close $out_fh;
    close $inf_fh;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Or something like this:

    Perl Code:
    while ( $line = <$inf_fh> ) {
         if ( $line =~ /^$mtch1/)   {
              foreach (1..2) {
                   my $outline = <$inf_fh>;
                   print $out_fh $outline;
             }
         }
    }


    (Assuming you don't want to print out the matching line. If you want to print also the matching line, just add one line to do it.)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    86
    Rep Power
    3
    Thank you very much for the code!
    It works great and it is written in a way I understand!

    I have one more question.
    I need to pull some data from a big file.

    Data split by X and Y coordinates and I need to pull the data by Xcoor and Ycoor
    I cannot predict the next part coordinates or the number of lines for each pare.
    Any idea how to get the lines for the part Xcoor_13/ Xcoor_14

    Code:
    Xcoor_13
    Ycoor_13
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    Xcoor_14 
    Ycoor_13 
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    Xcoor_17
    Ycoor_13
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Just read your Xcoor and Y coor. Then, read the next lines and, so long as it is not a new Xcoor, store your lines in whatever data structure suits your need, and, as soon as you get a new Xcoor, change the index of your data structure to start the same process again.

    You are not being specific enough about your data for me to be able to provide more than the general algorithm above, I can't suggest any code or even pseudo-code without having any idea of what your data actually looks like.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Try this:
    Code:
    my $out     =  '/temp/tmp/MLines.txt';
    my $inf     =  '/temp/tmp/t';
    my $line;
    
    open ( my $out_fh, '>', $out ) or die "Can't open $out $!\n";
    open ( my $inf_fh, '<', $inf ) or die "Can't open $inf $!\n";
    
    MAIN: while ( $line = <$inf_fh> ) {
      if ( $line =~ /^(Xcoor_13|Xcoor_14)/) {
        print $out_fh $line;
        while ( $line = <$inf_fh> ) {
          print $line;
          redo MAIN if ( $line =~ /^Xcoor/ );
          print $out_fh $line;
        }
      }
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    86
    Rep Power
    3
    Hi Spacebar,
    Thank you for the code it is much appreciated!

    Sorry, I see I was not clear on what I really need.
    If a user need all the lines for Xcoor_13 Ycoor_13
    Results:
    Code:
    Xcoor_13
    Ycoor_13
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    If a user need all the lines for Xcoor_14 Ycoor_13
    Results:
    Code:
    Xcoor_14 
    Ycoor_13 
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    If a user need all the lines for Xcoor_17 Ycoor_13
    Results:
    Code:
    Xcoor_17
    Ycoor_13
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    Now the script just prints:
    Code:
    Ycoor_13
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    Xcoor_14 
    Ycoor_13 
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    Xcoor_17
    Script
    Code:
    	open ( my $out_fh, '>', $out ) or die "Can't open $out $!\n";
    	open ( my $inf_fh, '<', $inf ) or die "Can't open $inf $!\n";
    	
    
    	MAIN: while ( $line = <$inf_fh> ) {
    	  if ( $line =~ /^(Xcoor_13|Xcoor_14)/) {
    	    print $out_fh $line;
    	    while ( $line = <$inf_fh> ) {
    	      print $line;
    	      redo MAIN if ( $line =~ /^Xcoor/ );
    	      print $out_fh $line;
    	    }
    	  }
    	}
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    oops , sorry, remove the below line I was using for testing; The data you want is in the output file(MLines.txt) so just cat the file to view:
    Code:
    print $line;
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    86
    Rep Power
    3
    Thanks for trying !
    It still prints:

    Code:
    Xcoor_13
    Ycoor_13
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    Xcoor_14 
    Ycoor_13 
    Some data here
    More data here
    Some data here
    More data here
    Some data here
    More data here
    Code:
    Code:
    open ( my $out_fh, '>', $out ) or die "Can't open $out $!\n";
    open ( my $inf_fh, '<', $inf ) or die "Can't open $inf $!\n";
    
    MAIN: while ( $line = <$inf_fh> ) {
      if ( $line =~ /^(Xcoor_13|Xcoor_14)/) {
        print $out_fh $line;
        while ( $line = <$inf_fh> ) {
          redo MAIN if ( $line =~ /^Xcoor/ );
          print $out_fh $line;
        }
      }
    }
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    That's because this line is only selecting(Xcoor_13 or Xcoor_14), you can change the selection or add to it:
    Code:
    if ( $line =~ /^(Xcoor_13|Xcoor_14)/) {
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Hi TestV,

    you are not saying enough about what you really need to do.

    What I would probably do with your data is to create an hash of arrays to store your data. The hash key would be, for example' 'Xcoor;Ycoor', and it would contain an array of the lines corresponding to these coordinates, so the structure would be something like this:

    HASH (X-Y coordinates)
    - 00 "some data"
    - 01 "some more data"
    - 02 "etc."

    Then when you need the values sor some XY coordinates, you can just list the hash values for these.

    But that's assuming that you will need the data for a certain duration. May be your needs are something different.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    86
    Rep Power
    3
    Hi Laurent,
    Thank you and Spacbar for helping me with this!
    The files I知 working with are very big, some of them more than 2M lines of data.
    A file is separated by x and y coordinates for each part (unit) tested.
    Each unit might have 50000 or more lines of data.
    I wanted to make a script that will print all the data for one part (unit) to a file with the Xccor and Ycoor in the beginning of the file (or at the end of the file).
    I知 not a programmer but I知 trying to write my own code and I learned a lot (thanks to you, Spacebar, Fish and other good people on this forum). I have never used HASH before and it looks scary but so was a Perl in the beginning.
    Thank you guys again for all the help I have now and always had on this forum!
    TesterV
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Hi TesterV, from your new description of what you want to do, you probably don't need to store your data in a hash as I suggested earlier (although frankly, there is nothing complicated about it).

    I now understand that you want to filter your data and split it into different files. Say, one file for CoorX/CoorY, and another file for a different set of coordinates. Did I get it right?
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    86
    Rep Power
    3
    That is correct!
  28. #15
  29. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Ok, Then I believe this is what you want to write each section to a different file:
    Code:
    my $out;
    my $outdir  =  '/temp/tmp/';
    my $inf     =  '/temp/tmp/t2';
    my $line;
    
    open ( my $inf_fh, '<', $inf ) or die "Can't open $inf $!\n";
    
    MAIN: while ( $line = <$inf_fh> ) {
      if ( $line =~ /^Xcoor/) {
        $out  =  substr( $line, 0, -1 );
        $out  =~ s/\s+$//;
        open ( my $out_fh, '>', "${outdir}${out}.txt" ) or die "Can't open ${outdir}${out}.txt $!\n";
        print $out_fh $line;
        while ( $line = <$inf_fh> ) {
          if ( $line =~ /^Xcoor/ ){
            close $out_fh;
            redo MAIN;
          }
          print $out_fh $line;
        }
      }
    }
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo