#!/usr/bin/perl
# get_lines.pl
use strict;
my $check_next_line = "no";
my ($new_line, $mp);
while (<>) {
chomp;
if (/^NEW/) {
$new_line = $_; # save the line beginning with word NEW
$check_next_line = "yes";
$mp = (split (/\s+/, $_))[2]; # save the field containing MP code
next;
}
elsif (/^CNL/) {
if ($mp eq (split (/\s+/, $_))[2]) {
print $new_line, "\n";
print;
print "\n";
}
}
$check_next_line = "no";
}
You mentioned the awk and Java. It looked like the perl script from Yogesh Sawant would work -- did you time it?
This looks mostly IO bound, so except for coding the algorithm, I would not expect drastically different times. For example, my experience is that perl is very close to c for IO cases, but not so close for arithmetic-dense code.
The perl might be made a bit more efficient by using the suffix "o" for matching constant patterns, and possibly not splitting more fields than needed -- but I'd think those are making very small contributions ... cheers, drl
Ah, you're suggesting it was rein's time, apparently. Yes, I could see how one's familiarity with the language at hand could affect time-to-solution ... cheers, drl
#!/usr/bin/python
data=open("file").readlines()
for num in range(len(data)):
if data[num].startswith("NEW") and data[num+1].startswith("CNL"):
if data[num].split()[2] == data[num+1].split()[2]:
print data[num],data[num+1]