We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,039 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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";		     
                  }
                              
      }
   
}
2
Contributors
4
Replies
3 Days
Discussion Span
2 Years Ago
Last Updated
5
Views
jatri
Newbie Poster
3 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

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.

jatri
Newbie Poster
3 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

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
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0967 seconds using 2.71MB