Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Facebook Twitter Instagram
Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Tech News Today
Home»Linux»How to Sort By Column In Linux

How to Sort By Column In Linux

Anup ThapaBy Anup ThapaAugust 15, 2022
linux sort by column

When dealing with a file that has a large number of entries, sorting the contents in a certain order can make things so much easier. In Linux, the sort command, which employs the merge sort algorithm, is used for this purpose.

The sort command compares all lines from the given files and sorts them in the specified order based on the sort keys. The -k option, in particular, is used to sort on a certain column.

In conjunction with the -k option, you’ll also need to know about other options like -n or -r to efficiently sort by column. We’ve detailed these and other related topics in the sections below.

Table of Contents

  • Sort By Column
    • Option -k
    • Option -n
    • Option -r
    • Option -t
    • Option -o
    • Additional Options

Sort By Column

The base syntax for the sort command is sort <option> <file>. To apply this, let’s start with an example. Let’s say we have a contacts.txt file with the following entries:

Emma Smith US 51
Edward Dyer UK 19
Natalie Russell Canada 22
Mathew Roberts Germany 21
Ruth Abraham Australia 44
Joseph Smith France 25

Just using the sort contacts command would sort the entries in alphabetical order based on the first column. To sort by another specific column, or in another order, you’ll have to use various options, which we’ve listed below.

Option -k

As stated, the --key, or -k option is used to sort on a specific column or field. For instance, in our example, if you wanted to sort by nationality, you’d use the following command to sort on the third column:
sort -k3 contacts

sort-k

This command will take all fields from 3 to the end of the line into account. Alternatively, if you used sort -k3,4 contacts, this would specify to use the fields from #3 to #4, i.e., the third to fourth fields.

If you were trying to sort by surnames in the second column, there are two scenarios to consider. As there are identical entries (Smith), sort will use the next entry for the tiebreaker. I.e., in the case of the two smiths, France would be sorted above US.

But what if you needed to sort by surname, name, and only then nationality? In such cases, you can manipulate the sort order by specifying keys multiple times as such:
sort -k2,2 -k1,1 contacts

sort-k-k

Option -n

If you use the commands shown above to sort by age on the fourth column, the results would seem inaccurate. As sort takes a lexicographic approach, fiftyone would rank above nineteen.

To sort a column by numbers, you have to instead specify the -n option as such:
sort -n -k4 contacts

sort-n-k

Option -r

If you were trying to sort entries in reverse order, you would use the -r option. This is true for both alphabetical and numeric values, as shown below:
sort -r -k1,1 contacts
sort -r -k3,4 contacts

sort-r-reverse

By combining the basic options we’ve listed so far, you could perform complex sorting operations like reverse sorting on the second column and numerically sorting on the fourth column at once:
sort -k2,2r -k4,4n contacts

sort-by-column-reverse-numeric

Option -t

A blank space is the default field separator, i.e., delimiter with sort. But CSV files use commas (,) to separate values. Depending on what delimiter is used, you can specify it using the -t option. For instance, if : is the delimiter, you would specify it as such:
sort -t ':' -k2 contact

sort-t-delimiter

Option -o

You can use the -o option to save the sorted output into a specified file as such:
sort -k2 contacts -o sortedcontacts

sort-o-save-output-to-file

Additional Options

The options detailed above are the most commonly used ones. But sort has countless other flags that could be useful in niche scenarios like -b to ignore blank spaces at the start of the file, -M to sort the contents as per the calendar month, or -c to check if data is already sorted. As there are too many to list here, we recommend referring to the sort man page for the full list of such options.

how-to
Anup Thapa
  • LinkedIn

Anup Thapa is a tech writer at TechNewsToday. He mostly writes informative articles, tutorials, and troubleshooting guides related to Windows systems, networking, and computer hardware. Anup has been writing professionally for almost 5 years, and tinkering with PCs for much longer. His love for all things tech started when he got his first PC over 15 years ago. It was a Pentium IV system running Windows XP on a single 256 MB stick. He spent his formative years glued to this PC, troubleshooting any hardware or software problems he encountered by himself. Professionally, Anup has had brief forays into a variety of fields like coding, hardware installation, writing, etc. In doing so, he's worked with people of different backgrounds and skill levels, from average joes to industry leaders and experts. This has given him not just a versatile skillset, but also a unique perspective for writing that enables him to concisely communicate complex information and solve his reader's problems efficiently. You can contact him at anup@technewstoday.com

Related Posts

how to change boot order on linux

How to Change Boot Order on Linux

December 16, 2022
make-command-not-found

How to Fix “Make: Command Not Found” Error

September 20, 2022
linux terminal for windows

How to Install Linux Terminal on Windows

September 19, 2022
How-to-force-Quit-an-app-on-Linux

How to Force Quit an App on Linux

August 30, 2022
best linux for virtualbox

8 Best Linux Distros to Try in VirtualBox

August 30, 2022
ifconfig command not found

How To Fix “ifconfig Command Not Found” Error?

December 18, 2022
Add A Comment

Leave A Reply Cancel Reply

Latest Posts
Memory-Compression

What is Memory Compression in Windows? Should You Enable or Disable It

March 1, 2023
dism-vs-sfc-vs-chdsk

DISM, SFC, CHKDSK: What’s the Difference

February 28, 2023
bios-settings-for-gaming

Best BIOS Settings for Gaming

February 16, 2023
You may also like
how-to-clean-hp-printer-rollers

How to Clean HP Printer Rollers

March 3, 2023
keyboard input lag

9 Ways to Fix Keyboard Input Lag

March 3, 2023
keyboard key is stuck

How to Fix a Stuck Key on a Keyboard

March 3, 2023
Recommended
Cookie Clicker Garden Guide

Cookie Clicker Garden Guide to Unlocking Every Seed

September 26, 2021
monitor no signal

Computer Turns On But Monitor Says No Signal (9 Ways To Fix)

November 10, 2022
Facebook Twitter Pinterest
  • Home
  • About Us
  • Our Team
  • Editorial Guidelines
  • Privacy Policy
  • Affiliate Disclosure
© 2023 TechNewsToday, editor@technewstoday.com | Tech Central Pvt. Ltd.

Type above and press Enter to search. Press Esc to cancel.