Archive: Sort a CSV file


Sort a CSV file
I'm trying to automate the sorting of a comma deliminated Excel file by a particular column. Any ideas?


I don't think it's possible using NSIS scripting (without System.dll), and if it is the script would be huge. Therefore I suggest gawk. It's a Win32 port of an Linux application. It's very powerful and with the right parameters can do what you've asked.

This example script sorts a file (and with the right tweaking I am sure it will be able to sort CSVs too):

{ 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.