Reputation: 1417
If I have a number of files, such as
1.txt:
1;ab, bc
2;cd, de, ef
3;fgh
2.txt:
4;bc
1;cd, ef
5;ab
2;g
3.txt:
5;ef, hl
7;a, b, c
3;k, jk
1;b
6;x
Assuming that ;
is a delimiter and the first column serves as ID, how to concatenate corresponding second columns (using eg. commas), so that the output becomes
output.txt:
1;ab, bc, cd, ef, b
2;cd, de, ef, g
3;fgh, k, jk
4;bc
5;ab, ef, hl
7;a, b, c
6;x
Upvotes: 0
Views: 86
Reputation: 67507
awk
to the rescue!
$ awk -F";" '{a[$1]=a[$1]?a[$1]","$2:$2}
END{for(k in a) print k";"a[k]}' file{1,2,3} | sort
1;ab, bc,cd, ef,b
2;cd, de, ef,g
3;fgh,k, jk
4;bc
5;ab,ef, hl
6;x
7;a, b, c
Upvotes: 2
Reputation: 875
cause join(1)
is designed for join two files, and the input has to be sort, so why bother. the awk source:
#!/usr/bin/env awk -f
BEGIN { FS = ";" }
FNR==NR { a[$1] = $2; next}
{
if ($1 in a) {
a[$1] = a[$1] ", " $2
} else {
a[$1] = $2;
}
}
END {
for (i in a) {
printf("%s%s%s\n", i,FS,a[i]);
}
}
Upvotes: 0