Connecting Tech Pros Worldwide Help | Site Map

How to parse files ...

Newbie
 
Join Date: Aug 2009
Posts: 2
#1: Sep 5 '09
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's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#2: Sep 5 '09

re: How to parse files ...


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
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Sep 5 '09

re: How to parse files ...


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.
Newbie
 
Join Date: Sep 2009
Location: earth
Posts: 3
#4: Sep 14 '09

re: How to parse files ...


Sorry, but simplest solution is:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $file = 'text.txt';
  5. open FILE, $file or die "can't open $file";
  6. my @text = <FILE>;
  7. close FILE;
  8.  
  9. my ( @ids ) = ( join '', @text ) =~ /hop (\d+)[^\d]+?9227493/g;
  10. printf "%s\n", ( join "\n", @ids );
  11.  
where text.txt contains user data
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,569
#5: Sep 14 '09

re: How to parse files ...


Quote:

Originally Posted by RichKersh View Post

Sorry, but simplest solution is:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $file = 'text.txt';
  5. open FILE, $file or die "can't open $file";
  6. my @text = <FILE>;
  7. close FILE;
  8.  
  9. my ( @ids ) = ( join '', @text ) =~ /hop (\d+)[^\d]+?9227493/g;
  10. printf "%s\n", ( join "\n", @ids );
  11.  
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
Reply