Interview Prep/Shell Scripting/Text processing: grep, sed, awk, cut, sort, uniq

How do you sort by a specific column, numerically, and in reverse?

BeginnerConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
sort -t, -k3,3n data.csv          # field 3, numeric, comma-separated
sort -k5,5nr report.txt           # field 5 numeric, descending
du -sh /var/* | sort -h           # human sizes: 512K < 3M < 2G
sort -u ips.txt                   # unique lines
sort -k2,2 -k1,1n file            # by field 2, then field 1 numeric
sort -V versions.txt              # version order: 1.9 < 1.10

Always give the end field (-k3,3); -k3 means "from field 3 to the end of line". Set LC_ALL=C for byte-order sorting that is fast and identical on every machine.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Text processing: grep, sed, awk, cut, sort, uniq