in reply to amalgamate similar lines
Assumptions:
Please forgive me for the rather tedious solution. I wanted to point out the importance of clearly and concisely stating the problem and assumptions.#!/usr/bin/perl use strict; use warnings; my $input = $ARGV[0] || 'sample.txt'; open(my $fh, '<', $input) or die "Unable to open $input for reading: $ +!"; my %data; while ( <$fh> ) { chomp; my @field = split /\|/, $_, 3; my $key = join '|', @field[0,1]; $data{$key}{line} = $. if ! exists $data{$key}; push @{ $data{$key}{records} }, $field[2]; } for ( sort { $data{$a}{line} <=> $data{$b}{line} } keys %data ) { if ( @{ $data{$_}{records} } > 1 ) { my $field3 = join ',', @{ $data{$_}{records} }; print join '|', $_, $field3; } else { print join '|', $_, $data{$_}{records}[0]; } print "\n"; }
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: amalgamate similar lines
by ysth (Canon) on Jan 09, 2006 at 14:05 UTC | |
by Limbic~Region (Chancellor) on Jan 09, 2006 at 14:11 UTC |