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";		     
                  }
                              
      }
   
}

Recommended Answers

All 4 Replies

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?

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 <FrNum: 1>. If my script runs properly then I should get the line that contains <FrNum: 1> and the next line.

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

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
    }
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.