Input file, testfile.csv:
:~ steve$ cat testfile.csv item 1,55,blah item 2,33,bleh item 3,8,blurgh item 4,494,something item 5,29,garbage item 6,49494,must get a proper CSV file for testing
Script:
set column_id to 2 set posix_path_to_csv to "/Users/steve/testfile.csv" set sorted_file to do shell script ("sort -n -t , -k " & column_id & " " & posix_path_to_csv)
Result: tell current application do shell script "sort -n -t , -k 2 /Users/steve/testfile.csv" --> "item 3,8,blurgh item 5,29,garbage item 2,33,bleh item 1,55,blah item 4,494,something item 6,49494,must get a proper CSV file for testing" end tell Result: "item 3,8,blurgh item 5,29,garbage item 2,33,bleh item 1,55,blah item 4,494,something item 6,49494,must get a proper CSV file for testing"
On 21 Jul 2010, at 11:42, Richard Lake wrote: Having trouble with this.. got a working example with "," as the separator? On 21 Jul 2010, at 11:17, Steve Thompson wrote: On 21 Jul 2010, at 10:26, Richard Lake wrote: I have a CSV like the extract below that I have read into Applescript in as an array: This is how I did something similar - I pre-process the CSV instead of sorting the array purely because it's faster. However, it's not much use if you need to sort the data by a different column at a later stage of your script
set sorted_file to do shell script ("sort -n -t , -k " & column_id & " " & posix_path_to_csv)
Just so you know, that's sort --numeric --use the next character as a separator , -sort this key"
|