Sort a CSV file
I'm trying to automate the sorting of a comma deliminated Excel file by a particular column. Any ideas?
2 posts
{ line[NR] = $0 "" } # make sure of comparison type
# in case some lines look numeric
END { isort(line, NR)
for(i = 1 ; i <= NR ; i++) print line[i]
}
#insertion sort of A[1..n]
function isort( A, n, i, j, hold)
{
for( i = 2 ; i <= n ; i++)
{
hold = A[j = i]
while ( A[j-1] > hold )
{ j-- ; A[j+1] = A[j] }
A[j] = hold
}
# sentinel A[0] = "" will be created if needed
}It's quite big (around 200KB for the EXE) so you might consider writing your own little program to do the job.