Friday, March 29, 2013

Sort,uniq (string, int) columns in csv files


cat file

text1,5
text4,3
text3,1
text1,4
text7,2
text3,7
text2,9


1. less file | sort -u -t, -k1,1 - sort the first column and get unique texts
text1,5
text2,9
text3,1
text4,3
text7,2

2. less file | sort -u -t, -k1,1 | sort -t, -k2 -nr - get unique texts on first column and reverse order for the second column using -r option
text2,9
text1,5
text4,3
text7,2
text3,1