How to parse files ...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • katwala
    New Member
    • Aug 2009
    • 2

    How to parse files ...

    I am trying to write a script to do the following.

    Go thru the file until it finds an expression ("9227493"). Once this expression is found, I want the script to search in a reverse fashion until it finds another string ("hopid"). And I want to save that line.

    How would I go about doing this?

    My file looks like this:

    First Line
    Second Line
    third line hop 2746
    fourth line
    fifth line
    sixth line
    seventh Line 9227493abc
    eighth Line
    nineth line hop 8888
    tenth line
    eleventh line 9227493abc
    twelvth line

    The output will be:
    2746
    8888

    Thank you in advance for your assistance.

    Kalpesh
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    If you don't know any perl I would start with a good perl book. Before others offer much assistance you will need to try and write the code yourself, or hire a programmer.

    If you have a problem with your code please post it back and we will see what can be done to help.

    --Kevin

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      What would be easier is to find hop, store the value you need, then keep going until you find 9227493 then print the value of hop. Repeat and continue until the end of the file.

      Comment

      • RichKersh
        New Member
        • Sep 2009
        • 3

        #4
        Sorry, but simplest solution is:
        Code:
        use strict;
        use warnings;
        
        my $file = 'text.txt';
        open FILE, $file or die "can't open $file";
        my @text = <FILE>;
        close FILE;
        
        my ( @ids ) = ( join '', @text ) =~ /hop (\d+)[^\d]+?9227493/g;
        printf "%s\n", ( join "\n", @ids );
        where text.txt contains user data

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by RichKersh
          Sorry, but simplest solution is:
          Code:
          use strict;
          use warnings;
          
          my $file = 'text.txt';
          open FILE, $file or die "can't open $file";
          my @text = <FILE>;
          close FILE;
          
          my ( @ids ) = ( join '', @text ) =~ /hop (\d+)[^\d]+?9227493/g;
          printf "%s\n", ( join "\n", @ids );
          where text.txt contains user data
          While we definitely appreciate your helping out, this is a learning forum. The op has already been asked to provide their code if they got stuck, which tells them they need to at least try to code a solution.

          Regards,

          Jeff

          Comment

          Working...