Perl: Search for string then parse next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Search for string then parse next line
# 1  
Old 04-06-2006
Perl: Search for string then parse next line

Hi All,

I have a file that I need to be able to find a pattern match on one line then parse data on the next or subsequent lines - I will know which line needs to be parsed beforehand.

This is what I currently have:

Code:
while (<COMMAND_OUT>) {

   if ($_ =~ m/TEST/) {

      $enabled_yn{IMSI} = substr($_,-17,15) ;
   }

   if ($_ =~ m/BAOC/) {

      $enabled_yn{BAOC} = substr($_,-3,2) ;

   }

   if ($_ =~ m/BOIC/) {

      $enabled_yn{BOIC} = substr($_,-3,2) ;

   }
}

This works find when the string that I need to parse is on the same line but wont work when the string that I need to capture is on the next line.

I think the answer lies in manipulating $. but I'm unsure how to do this...

Many thanks,

pondlife.
# 2  
Old 04-06-2006
Not very familiar with perl. Only a kludge.

Code:
$found = 0;

while (<COMMAND_OUT>) {

   if ($found == 1) {
   # get to work on the line.
   $found = 0;
   }

   if ($_ =~ m/TEST/) {
      $enabled_yn{IMSI} = substr($_,-17,15) ;
      if string-is-on-next-line {
         $found = 1;
      }
   }

   if ($_ =~ m/BAOC/) {
      $enabled_yn{BAOC} = substr($_,-3,2) ;
   }

   if ($_ =~ m/BOIC/) {
      $enabled_yn{BOIC} = substr($_,-3,2) ;
   }
}

# 3  
Old 04-06-2006
Hi Vino,

Thanks for the reply. The string I'm looking for is only on the previous line and not on the line that I want to parse. This is an example of my file:

Code:
BORO ... SOME RANDOM AMOUNT OF TEXT APPEARS HERE
         AND MORE TEXT APPEARS HERE ...................... N

In this instance I'm searching for BORO but I want to extract the N (if it is an N - this can change) on the end of the second line.

Hope this is clearer.

Many thanks,

pondlife.
# 4  
Old 04-06-2006
See this.

Code:
#! /usr/local/bin/perl

open (TEXT_FILE, "</tmp/headers.txt"); 

while ($line = <TEXT_FILE>) {
        if ($line =~ m/BORO/) {
                $nextline = <TEXT_FILE> ;
                printf ("$line $nextline");
        }
}
close (TEXT_FILE);

After your match is found in $line, $nextline gets the next line from the input file. Your desired match should be found in $nextline.
# 5  
Old 04-06-2006
Genius!

Vino - you're a star! It works a treat...

My actual code looks like this (for those who are interested):
Code:
   if ($_ =~ m/BOIH/) {

      $nextline = <COMMAND_OUT> ;
      $nextline1 = <HLRCOMMAND_OUT> ;
      $nextline2 = <HLRCOMMAND_OUT> ;
      $enabled_yn{BOAC} = substr($nextline2,-3,2) ;

   }

This enables me to chose how many lines later I wish to parse the data.

Cheers, pondlife.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

2. Shell Programming and Scripting

Perl :: to parse the data from a string.

Hi folks, I have a line in log from which I need to parse few data. Jul 6 00:05:58 dg01aipagnfe01p %FWSM-3-106011: Deny inbound (No xlate) From the above... I need to parse the %FWSM-3-106011: substring. Another example Jul 13 00:08:55 dq01aipaynas01p %FWSM-6-302010: 2 in use, 1661... (3 Replies)
Discussion started by: scriptscript
3 Replies

3. Programming

Perl parse string

Hi Perl Guys I have another perl question I have the following code that i have written Getopt::Long::config(qw( permute bundling )); my $OPT = {}; GetOptions($OPT, qw( ver=s help|h )) or die "options parsing failed"; This will allow the user to do something like... (4 Replies)
Discussion started by: ab52
4 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

Search string and parse

Input file 0792 to 2450 iadmssql7: Copy: CNJ R1: Replication volumes: Replication set: RSet 1 Replication size: 200.00GB SAN Info: 200.00GB DGC VRAID CX4-960 LUN 17 (17) RPA Port WWN Ctrl ... (0 Replies)
Discussion started by: greycells
0 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

Perl parse string to time

Hi, I have got this value 18:21:23.330 in one of my variables. Now I need to parse this time to something. And then I have to compare it with 2 times, let's say, 15:00 hrs to 23:00 hrs. Can Date::Manip rescue me from this horrifying situation? I am quite new to Perl and especially this... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then compare numbers!

Hi All, I have a file that I need to be able to find a pattern match on a line, take the number on that line check if its >0.9 or <0.1 and if this is true write the line to output.out file. An example of 4 lines in my file is: 1. driver.I177.I11.net010 1.48622200477273e-05 2.... (2 Replies)
Discussion started by: Crypto
2 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. UNIX for Dummies Questions & Answers

Search and Parse string from inside a File

Hello, I barely know the basics, but I am very determined to learn. I want to parse a few characters from each row, use that string to search another file and display the line number where I found the value in the file. I don't know if this can all be done on the command line, so I am creating a... (2 Replies)
Discussion started by: SSims
2 Replies
Login or Register to Ask a Question