Linux - Sort by multiple columns

By xngo on June 30, 2019

cat people.txt
#   john; smith; 49
#   amanda; bush; 55
#   john; kidman; 55
#   john; bush; -9
#   amanda; bush; 1
 
# Sort by the 3rd column
sort -t ';' -k3,3n people.txt
#   john; bush; -9
#   amanda; bush; 1
#   john; smith; 49
#   amanda; bush; 55
#   john; kidman; 55
 
# Sort by 2nd, 1st and then 3rd column
sort -t ';' -k2,2 -k1,1 -k3,3n people.txt
#   amanda; bush; 1
#   amanda; bush; 55
#   john; bush; -9
#   john; kidman; 55
#   john; smith; 49

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.