Bash

Sort by a Specific Column

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Sort takes -k for column-based ordering and -t to set the delimiter. -h sorts human-readable sizes ("1.5G", "300K") correctly.
Bash
Raw
# Sort by 3rd whitespace-delimited column, descending numeric
sort -k3 -rn data.txt

# CSV: -t sets the field separator
sort -t',' -k2 -rn transactions.csv

# Sort by 2nd, then by 1st column as tiebreaker
sort -k2,2 -k1,1 -t',' users.csv

# Human-readable sizes
du -sh /var/log/* | sort -h         # smallest → largest
du -sh /var/log/* | sort -rh        # largest → smallest

# Sort by date column in YYYY-MM-DD format
sort -k4 events.log                 # works because ISO dates sort alphabetically
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.