Operating System - HP-UX
1834139 Members
2234 Online
110064 Solutions
New Discussion

Re: Sorting data based on criteria in Perl

 
SOLVED
Go to solution
Jeff Colyer
Advisor

Sorting data based on criteria in Perl

I have a data file that contains a set of "batches" that I need to grab out records that meet a certain search criteria. The catch is I need to tie the records back to the batch header. Here's an example of the data file.

batchname|BNO123|BDA2006-02-19|
REC|DNO11|PNO1111|UPR0999
REC|DNO11|PNO1114|UPR0999
REC|DNO11|PNO1116|UPR0999
batchname2|BNO124|BDA2006-02-19|
REC|DNO11|PNO1112|UPR0999
REC|DNO11|PNO1111|UPR0999
REC|DNO10|PNO1111|UPR0999

I need to be able to sort by both DNO and PNO as they aren't unique. A sort where DNO=11 and PNO=1111 should yield this results:
batchname|BNO123|BDA2006-02-19|
REC|DNO11|PNO1111|UPR0999
batchname2|BNO124|BDA2006-02-19|
REC|DNO11|PNO1111|UPR0999

I'll be passing the search parameters in through a shell script but I'm a perl newbie and am having difficulty trying to use hashes to parse the data. Thanks in advance!
12 REPLIES 12
Muthukumar_5
Honored Contributor

Re: Sorting data based on criteria in Perl

Simply as,

# grep -E '^batchname|DNO11\|PNO1111' filename

will give results.

Change DN011\|PNO1111 based on requirement.


--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Sorting data based on criteria in Perl

With perl simply as,

perl -ne 'print if /^batchname|DNO11\|PNO1111/' filename


change DN and PNO part based on your requirement.

--
Muthu
Easy to suggest when don't know about the problem!
Jeff Colyer
Advisor

Re: Sorting data based on criteria in Perl

Sorry I should've been more clear. The batchname could start with anything. There is no standard naming convention. I need a way to associate the batch header with the record as the batch header could be associated with hundreds of records and there could also be 30 or more batches in one data file.

- Jeff
Muthukumar_5
Honored Contributor

Re: Sorting data based on criteria in Perl

oops. Change batchname to,

perl -ne 'print if /BNO[0-9]*|DNO11\|PNO1111/' filename

--
Muthu
Easy to suggest when don't know about the problem!
Jeff Colyer
Advisor

Re: Sorting data based on criteria in Perl

This actually listed every single batch header in the file and no records.

-Jeff
Jeff Colyer
Advisor

Re: Sorting data based on criteria in Perl

I'm going to add an actual data file for clarity. It's from a unix system. The field delimeter is actually /375. I provided the above for simplification.
Muthukumar_5
Honored Contributor

Re: Sorting data based on criteria in Perl

I could not understand the file delimiter in,

050269ýBNO426823ýBDA2006-02-19ýBTI080303ýEFD2006-02-18ýEFT00:01ýSRB0ýBTYOýRNO14ýSNO100ý

/375? what it is?

--
Muthu
Easy to suggest when don't know about the problem!
Jeff Colyer
Advisor

Re: Sorting data based on criteria in Perl

It was late when I typed that. It's actually \375. I'm not sure why this was picked for a delimeter it's just in the data that is given to me. (I believe it has something to do with a vendor built driver) That's why I tried to simplify and use a | for the example. If it's easier you can do a sed and replace \375 with a | on the data I attached. Thanks again!
Jeff Colyer
Advisor

Re: Sorting data based on criteria in Perl

Also, to see the \375 delimeter you must vi the file on a unix system. If you try to cat or open in notepad it garbles the delimeter.

-Jeff
Rodney Hills
Honored Contributor
Solution

Re: Sorting data based on criteria in Perl

If you already have DNO and PNO in shell variables, then may this script will do what you are looking for-

$dno="DNO".$ENV{DNO}; $pno=PNO.$ENV{PNO};
while(<>) {
chomp;
@a=split("\375",$_);
if (substr($a[1],0,3) eq "BNO") {
$hdr=$_;
} else {
if ($a[1] eq $dno and $a[2] eq $pno) {
print $hdr,"\n";
print $_,"\n";
}
}
}

Then run with-
perl thisscript yourinputfile >resultfile

HTH

-- Rod Hills
There be dragons...
Jeff Colyer
Advisor

Re: Sorting data based on criteria in Perl

Thanks Rod, that worked perfectly.
Jeff Colyer
Advisor

Re: Sorting data based on criteria in Perl

If you already have DNO and PNO in shell variables, then may this script will do what you are looking for-

$dno="DNO".$ENV{DNO}; $pno=PNO.$ENV{PNO};
while(<>) {
chomp;
@a=split("\375",$_);
if (substr($a[1],0,3) eq "BNO") {
$hdr=$_;
} else {
if ($a[1] eq $dno and $a[2] eq $pno) {
print $hdr,"\n";
print $_,"\n";
}
}
}

Then run with-
perl thisscript yourinputfile >resultfile

HTH

-- Rod Hills