Mac OS X
Q: Sort csv file with Unix sort in Applescript
I am trying to sort a csv file on field 5 using unix sort but nothing happens, where am I going wrong? Any help gratefully received!
set filePath to quoted form of POSIX path of "/Users/Ours/Documents/Test File.csv"
do shell script "sort -t ',' -k5 " & filePath & " -o " & filePath
Posted on Aug 26, 2016 8:54 AM
by Hiroto,Solvedanswer
Hello
Most likely your csv file uses CR as line ending where sort(1) expects LF.
Try something like the following script, which will translate CRLF and CR to LF in source text before passing it to sort(1).
set f to "/Users/Ours/Documents/Test File.csv" do shell script "f=" & f's quoted form & " perl -lp0e 's/\\015\\012|\\015/\\012/og;' \"$f\" | sort -t, -k5 -o \"$f\""
Regards,
H
Posted on Aug 27, 2016 4:33 AM