For small files read the file a line at a time and add the lines to a hash.
When you have read a line first check that it's not in the hash already. If not, print it to the output file and insert it into the hash, otherwise skip to the next line.
If you can't figure that out, write some code that is your best guess at how to do it, ask again and include your code.
Provide half a dozen lines of sample data, the test code you are currently using, and a sample of the output you expect to see.
For the test code it is easiest to use a __DATA__ section for the test data rather than an external file and simply print the result rather than generating an external file.
The following is ok for reasonable size files but may bog down when things get huge.
use strict;
use warnings;
use Data::Dump::Streamer;
my %firstLines;
my @lines;
while () {
chomp;
my ($data, $type) = /(.*)\s+TYPE:\s+(\w+)$/;
next if ! defined $type; # ignore malformed line
if (exists $firstLines{$data}) {
$lines[$firstLines{$data}] .= ", $type";
} else {
$firstLines{$data} = @lines;
push @lines, $_;
}
}
print join "\n", @lines;
__DATA__
MCAT: 0xf30cbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA0
MCAT: 0xcc3fbed1 PCAT: 0x000fb109 LMAT: 0x00000800 TYPE: KA1
MCAT: 0xeeccbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA1
MCAT: 0xf30cbe91 PCAT: 0xafaddd09 LMAT: 0x00040000 TYPE: KA0
MCAT: 0xeeecbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA0
MCAT: 0xcc000331 PCAT: 0x000fb109 LMAT: 0x00000800 TYPE: KA1
MCAT: 0xe554be01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA1
MCAT: 0xf30cbe91 PCAT: 0xafaddd09 LMAT: 0x00040000 TYPE: KA1
Prints:
MCAT: 0xf30cbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA0 MCAT: 0xcc3fbed1 PCAT: 0x000fb109 LMAT: 0x00000800 TYPE: KA1 MCAT: 0xeeccbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA1 MCAT: 0xf30cbe91 PCAT: 0xafaddd09 LMAT: 0x00040000 TYPE: KA0, KA1 MCAT: 0xeeecbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA0 MCAT: 0xcc000331 PCAT: 0x000fb109 LMAT: 0x00000800 TYPE: KA1 MCAT: 0xe554be01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA1
Just in case there is some confusion or misunderstanding of some of the Perl tricks used I better go through some of that code and elaborate on what's happening. Note that I've taken interesting lines in processing order rather that the order they are coded.
$firstLines{$data} = @lines;this is a little tricksy. It creates a new entry in %firstLines that contains the index to the new line as the value and is keyed by the unique part of the line contents. @lines in scalar context returns the number of elements in the array.
if (exists $firstLines{$data})checks to see if we've already seen a specific line.
$lines[$firstLines{$data}] .= ", $type";builds the multiple entries for duplicated lines. Note that $firstLines{$data} returns the index number that was stored earlier.
sort -u infile > outfile # or (you might many some of uniq's extra options): sort infile | uniq > outfile
my $last = $_ = <>;
print;
while (<>)
{
print if ($_ ne $last);
$last = $_;
}
Also note that, of the solutions provided thus far, the hash-based option is the only one which will both eliminate all duplicates (printing only the first appearance of each line) and also preserve the original order of the (remaining) lines, which may or may not be significant to you.
perlmonks.org content © perlmonks.org and Anonymous Monk, davidrw, esper, GrandFather, ibeneedinghelp
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03