Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get the next line of a particular line?

Status
Not open for further replies.

kobewins

Programmer
Dec 1, 2005
57
US
Hi,

I'm using the following File infile1 (sam_rpt) to get the line number ($ln) for File infile2 (loadPracNew.in), ie, the first number after 'Error Text: ' in infile2. In my example, $ln will be 1, and 5.

#### Infile1: sam_rpt

LOAD0007
RUN DATE: Dec 13, 2005 12:07:42

PRIMARY KEY

Error Text: 1, , 12, DOCDEA, , PRACTITIONER_REGI ...
Error Text: 5, , 12, DOCDEA1, , PRACTITIONER_REG ...

RECORDS READ: 6 ACCEPTED: 2


#### Input File 2: loadPracNew.in

PCDOCFIRSTNAME DOCLASTNAME DOCDEA 12PRE194001013000010
AISTATELICID 082005120130000101
PCDOCFIRSTNAME1 DOCLASTNAME1 DOCDEA1 32PRE195001013000010
AISTATELICID1 082005120130000101
PIDOCFIRSTNAME DOCLASTNAME STATELICID 58PRE194001013000010
PIDOCFIRSTNAME1 DOCLASTNAME1 STATELICID1 68PRE195001013000010

Then, I need to loop through infile2, read the line ($ln) into an output file (newLoad.dat).

My problem is: if the next line of Line $ln (Line $ln+1) begins with 'A', both Line $ln and Line $ln+1 should be read into the output file. So my output file should look like the following (from Line 1, Line 1+1, and Line 5 of infile2). But I don't know how to get the Line $ln+1. Please help.

#### Output File: newLoad.dat

PCDOCFIRSTNAME DOCLASTNAME DOCDEA 12PRE194001013000010
AISTATELICID 082005120130000101
PIDOCFIRSTNAME DOCLASTNAME STATELICID 58PRE194001013000010


Many thanks
David
 
Sorry, I have forgot to attach what I have so far.

David


----

#!/usr/bin/perl

my @list = `grep "Error Text:" sam_rpt`;

open(OUTFILE, "> newLoad.dat")
or die "Can't open newLoad.dat for writing: $!\n";

foreach(@list)
{
/Error Text:\s*(\d+)/;
my $ln = $1;
print "Line no. $ln\n";

open(INFILE, "loadPracNew.in") or die "Can't open file for reading: $!\n";
while (<INFILE>) {

$line = $_;
last if $. == $ln; # read a particular line into $line
}
if ($. != $ln) {
die "Didn't find line $ln in input file: loadPracNew.in\n";
}

#write that line ($line) into a new ourput file
print OUTFILE ($line);
close(INFILE);
}

close(OUTFILE);

----
 
Hiya,

# do what you need to with current line first, and then
if(/^A/){
# get next line, into $_ as usual
[tab]$_=<INFILE>;
# now do something with it
}

Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top