James Edward Gray II wrote: > > On Saturday, October 5, 2002, at 06:02 PM, Paul Van Dalen wrote: > > > > Given this input: > > > > 222 xxx JJJJJ > > 222 www DD > > 222 cc ffFFF > > 909 eee EEEE > > 909 FFF kkkk > > 909 jjjjj KKKK > > 888 JJJ HHHH > > 888 HHH JJJJ > > > > I'd like to split the input, group it by the first column, and, for that > > group, print out a concatenated second column. The output should look > > thusly: > > > > 222 xxx;www;cc > > 909 eee;FFF;jjjjj > > 888 JJJ;HHH > > perl -pe 's/^(\d+\s+)([a-zA-Z]+)\s+([a-zA-Z]+)$/$1$2;$3/' infile >outfile
$ perl -pe 's/^(\d+\s+)([a-zA-Z]+)\s+([a-zA-Z]+)$/$1$2;$3/' infile 222 xxx;JJJJJ 222 www;DD 222 cc;ffFFF 909 eee;EEEE 909 FFF;kkkk 909 jjjjj;KKKK 888 JJJ;HHHH 888 HHH;JJJJ This _will_ work: :-) $ perl -ane' BEGIN{$prev = ""} END{print "@$_\n" for @data} if ($prev eq $F[0]) {$data[-1][1] .= ";$F[1]"} else {$prev = $F[0], push @data, [@F[0,1]]}' infile 222 xxx;www;cc 909 eee;FFF;jjjjj 888 JJJ;HHH And if you don't care what order the records are output. $ perl -lane'push@{$d{$F[0]}},$F[1]}{print"$_ ",join";",@{$d{$_}}for keys%d' infile 222 xxx;www;cc 909 eee;FFF;jjjjj 888 JJJ;HHH John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]