sorting a csv file with 3 columns
I am trying to sort a csv file containing 3 values. The first one is the one that matters. It contains many times the number 1, 2, 3, 4 etc etc. But each number doesnt appear the same number of times as the other number. So, i wanna sort the csv file from its 1st column, from less times of appearance of each number to the highest number of appearces.
for example - i dont mention the 2nd and 3rd row because i want it sorted via the 1st column
1
1
1
2
2
2
2
2
3
4
4
->
3
4
4
1
1
1
2
2
2
2
using mac but i can get access to a linux
here is the file: http://www.mediafire.com/file/v0azlrb3sx9r5x7/example.csv/file
Try this,
awk 'BEGIN{FS=OFS=","} NR==FNR{s[$1]++} NR>FNR{print s[$1],$0}' csvfile csvfile \
| sort -n | cut -d, -f2-
-
awk
will count number of occurrences of first field (s[$1]++
) and then adds it (print s[$1],$0
to the rows). (Note, thatawk
needs to read the file twice). -
sort -n
will sort the result -
cut -d, -f2-
will remove the number again.