954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Current and next line printing

I would like to match a pattern from the following data set and print the line containing that pattern plus the immediate next line. Say I am interested in matching the pattern- FrNum: 1. If my program runs correctly for the following data set, the output should be :

GOP: 6 FrNum: 1 FrTyp: I
GOP: 6 FrNum: 10 FrTyp: P .

GOP: 67 FrNum: 1 FrTyp: I
GOP: 67 FrNum: 9 FrTyp: B

Data Set

GOP: 3 FrNum: 7 FrTyp: P
GOP: 3 FrNum: 14 FrTyp: B
.
GOP: 6 FrNum: 2 FrTyp: B
GOP: 6 FrNum: 6 FrTyp: B
.
GOP: 6 FrNum: 1 FrTyp: I
GOP: 6 FrNum: 10 FrTyp: P .
.
GOP: 31 FrNum: 8 FrTyp: B
GOP: 31 FrNum: 11 FrTyp: B
.
GOP: 32 FrNum: 8 FrTyp: B
GOP: 32 FrNum: 10 FrTyp: P
.
GOP: 36 FrNum: 2 FrTyp: B
GOP: 36 FrNum: 13 FrTyp: P
.
GOP: 60 FrNum: 4 FrTyp: P
GOP: 60 FrNum: 5 FrTyp: B
.
GOP: 67 FrNum: 1 FrTyp: I
GOP: 67 FrNum: 9 FrTyp: B

---------------------------

I am new to perl and so far the code I have written is not working. What changes do I need to make?

#!/usr/bin/perl -w

my  $next1;

print "Please Enter Input File\n\n";
$input_file = <STDIN>;
open(INFILE,  $input_file)   or die "Can't open $input_file: $!";

while (<INFILE>)
{
                    
    if($_ =~ /GOP:\s([0-9]+)\tFrNum:\s([0-9]+)\tFrTyp:\s([A-Z])/)

    {
                         	  
	  	  
	  $_     = $next1;
	
	  
                    if ($3 == 1)  
                    
                  {
                     print "$_";
                     print "$next1.\n";		     
                  }
                              
      }
   
}
jatri
Newbie Poster
3 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Tell us what you mean by not working. Do you get a syntax or runtime error, or does it print too many lines or nothing at all?

Give us an example that's easy to test. You don't want to have to type the name of the file every time you test your script, do you? Once you know the file-prompting portion of your script works, set it aside and make a testable snippet of the record-matching and printing part of the script so you can test it repeatedly and debug it.

I see at least one problem in your script:

my  $next1;#Declare variable
#Do other things
#...
#$next1 still has no value
$_     = $next1;#Wipe out the current record by assigning no value. Why?
print "$_";#Print nothing?
print "$next1.\n";#Print nothing followed by period followed by newline?
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

Thanks for your reply. I don't get any output at all.

Just want to print the current line (that contains a matching) and the immediate next line from the data set!

Say I would like to match . If my script runs properly then I should get the line that contains and the next line.

I am sure I'm doing something stupid but as I am new to perl, can't locate it.

jatri
Newbie Poster
3 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

I suggest that the part of your code that loops through the lines should look more like this:

my $counter = 0; #Number of lines you want to print

while (<INPUT>){
    $counter = 2 if m/FrNum: 1 /;
    if ($counter > 0){
        print;
        $counter--;#Subtract 1 from your counter
    }
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

Thank you very much. I will try to follow your suggestion and see how it goes.

jatri
Newbie Poster
3 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: