1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
BEGIN {
TAGS_SEP = ", ";
}
{
arr[NR] = reconstruct($0);
}
END {
for (i in arr)
print arr[i];
}
function reconstruct(line, tags) {
gsub(/, +/, TAGS_SEP, line)
split(line, tags, TAGS_SEP);
quick_sort(tags, 1, length(tags));
return join(tags, 1, length(tags), TAGS_SEP);
}
function quick_sort(array, left, right, i, j, tmp, pivot) {
if (left < right) {
i = left;
j = right;
pivot = array[int((left + right) / 2)];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
}
quick_sort(array, left, j);
quick_sort(array, i, right);
}
}
function join(array, start, end, sep, result, i) {
if (sep == "") {
sep = " ";
} else if (sep == SUBSEP) {
sep = "";
}
result = array[start];
for (i = start + 1; i <= end; i++) {
result = result sep array[i];
}
return result;
}
|