General discussion

Locked

sort merge

By srinyd ·
Hi, I have a question on sorting and merging two files. I have two files with the following data

File A
******
File A
******
aaaa 5 aaaa aaa 888 aaa
gggg 5 gggg ggg 555 ggg
bbbb 5 bbbb bbb 444 bbb
rrrr 5 rrrr rrr 999 rrr

File B
******
tttt 10 tttt ttt 444 ttt
yyyy 10 yyyy yyy 666 yyy
ffff 10 ffff fff 111 fff
kkkk 10 kkkk kkk 999 kkk
cccc 10 cccc ccc 444 ccc
zzzz 10 zzzz zzz 234 zzz
vvvv 10 vvvv vvv 888 vvv

I need the out put in the following format

bbbb 5 bbbb bbb 444 bbb
tttt 10 tttt ttt 444 ttt
cccc 10 cccc ccc 444 ccc
gggg 5 gggg ggg 555 ggg
aaaa 5 aaaa aaa 888 aaa
vvvv 10 vvvv vvv 888 vvv
rrrr 5 rrrr rrr 999 rrr
kkkk 10 kkkk kkk 999 kkk
yyyy 10 yyyy yyy 666 yyy
ffff 10 ffff fff 111 fff
zzzz 10 zzzz zzz 234 zzz

merge Files A & B by groupin on the feild 5 and sort the feild 2 so that the number 5 (from file A) in the feild comes first and number 10 (from the files B) comes next.

Please let me know if any one has ideas.

Thank you so much in advance
Srini

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by Nico Baggus In reply to sort merge

The following perlscript should do the trick....
---8<---
#!perl -w
my %store;
open(A,"<".$ARGV[0]) || die("Cannot open file
A");
open(B,"<".$ARGV[1]) || die("Cannot open file
B");
while ($line = <A&gt {
my @fields = split(/ /, $line);
if (defined (
$store{$fields[4]}{$fields[1]})) {

$store{sprintf("%010d",$fields[4])}{sprintf("%010d",$fields[1])}
.= $line; # append equal values....
} else {

$store{sprintf("%010d",$fields[4])}{sprintf("%010d",$fields[1])}
= $line;
}
}
close A;
while ($line = <B&gt {
my @fields = split(/ /, $line);
if (defined (
$store{$fields[4]}{$fields[1]})) {

$store{sprintf("%010d",$fields[4])}{sprintf("%010d",$fields[1])}
.= $line; # append equal values....
} else {

$store{sprintf("%010d",$fields[4])}{sprintf("%010d",$fields[1])}
= $line;
}
}
close B;

foreach my $f5 (sort keys %store) {
foreach my $f2 (sort keys %{$store{$f5}})
{
print $store{$f5}{$f2};
}
}

Collapse -

by ghislain.levesque In reply to sort merge

sort -o file_x +4 -5n +1 -2n filea fileb

file_x will contain the result

Back to Linux Forum
2 total posts (Page 1 of 1)  

Related Forums