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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print duplicate entry of array that matches reg expression

Status
Not open for further replies.

salkeld

Technical User
Mar 28, 2008
1
GB
I'm a newbie to Perl and I've been struggling with Google to get a answer.

The first field in the array is the key. Upon finding a duplicate key (not an entire line just the first field).Only print out the entire record if the 6th field is not equal to "Prod"

For example in the sample below 844147f2 is repeated twice. But I only want to print out the line that does not have "prod" as the value of the 6th field.

array data (2000 lines) sample:

844147f2| rdcbackn6| SUNW,Sun-Fire-V490| Sun Fire V490| EMSBCK| IDENinfra| 5.10| U1p1
844147f2| rdcbackn6| SUNW,Sun-Fire-V490| Sun Fire V490| EMSBCK| Prod| 5.10| U1p1
84414840| olibackn3| SUNW,Sun-Fire-V490| Sun Fire V490| EMSBCK| IDENinfra| 5.10| U1p1
84414840| olibackn3| SUNW,Sun-Fire-V490| Sun Fire V490| EMSBCK| Prod| 5.10| U1p1
84414860| rdcbackn1| SUNW,Sun-Fire-V490| Sun Fire V490| EMSBCK| IDENinfra| 5.10| U1p1
84414860| rdcbackn1| SUNW,Sun-Fire-V490| Sun Fire V490| EMSBCK| Prod| 5.10| U1p1
84414866| rdcbackn8| SUNW,Sun-Fire-V490| Sun Fire V490| EMSBCK| IDENinfra| 5.10| U1p1
84414866| rdcbackn8| SUNW,Sun-Fire-V490| Sun Fire V490| EMSBCK| Prod| 5.10| U1p1


Thanks in advance
J
 
Which piece are you struggling with? Finding the duplicates, identifying 'Prod', reading the data into the array?
 
If I were you I would sort the file first (using, e.g. the Unix sort utility).

Then, if it is sorted, you could say in Perl:

Code:
use strict;

my ($sPreviousId, $sPreviousLine) = ('', '');
my ($iId, $sCol6);

while( <> ) {
	($iId, $sCol6) = (split(/\| /))[0,5];

	print unless( $sPreviousId eq $iId && $sCol6 eq 'Prod' );

	($sPreviousId, $sPreviousLine) = ($iId, $_);
}

Though this admittedly only works if the other values in the 6th column are alphabetically before 'Prod' (as it happens to be the case in your example with 'IDENinfra')
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top