By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,692 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,692 IT Pros & Developers. It's quick & easy.

How to parse files ...

P: 2
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:

Thank you in advance for your assistance.

Sep 5 '09 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 971
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.

Sep 5 '09 #2

Expert 2.5K+
P: 4,059
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.
Sep 5 '09 #3

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

Expert Mod 2.5K+
P: 3,503
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.


Sep 14 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.