Skip to main content

Sorting Tabular Data with Dates

Sorting tabular data with dates in the MM/DD/YYYY format using the sort command in Linux involves specifying the right field and characters as sort keys. This article will guide you through creating a sample file with tabular data containing dates and then using sort to organize this data by the date column.

Step 1: Creating a Sample File​

First, we'll create a file named date_data.txt using vim. Here are the commands you would use:

  1. Open Terminal.
  2. Type vim date_data.txt and press Enter to create and open the file in vim.
  3. Press i to switch to insert mode.
  4. Type or paste in the following data:
John Doe,01/23/2021,Developer
Jane Smith,03/17/2020,Manager
Alice Johnson,12/11/2019,Analyst
Bob Brown,07/08/2022,Consultant
  1. Press Esc to exit insert mode.
  2. Type :wq and press Enter to write the file and quit vim.

This file date_data.txt includes names, dates in the MM/DD/YYYY format, and job titles, separated by commas.

Step 2: Sorting the File by Date​

Now we'll sort the file by the date column using the sort command. We'll specify the key field and use offsets to accurately target the month, day, and year within the date format.

sort -t, -k2.7,2.10 -k2.1,2.2 -k2.4,2.5 date_data.txt

Here's what the options mean:

  • -t,: This sets the delimiter to a comma, which is what separates our columns.
  • -k2.7,2.10: This defines the primary sort key from the 7th to the 10th character of the second field (which corresponds to the year in our date format).
  • -k2.1,2.2: This sets the secondary sort key to the first and second characters of the second field (which are the month in our date format).
  • -k2.4,2.5: This sets the tertiary sort key to the fourth and fifth characters of the second field (which are the day in our date format).

The file will be sorted primarily by year, then by month, and then by day, which is the natural chronological order for dates.


Sorting tabular data by dates requires careful consideration of the date format. By using the -k option with specific character positions, sort can handle dates in virtually any arrangement. For this demonstration, we prepared data in a common MM/DD/YYYY format and used sort with field and character offsets to order the entries by date accurately.

Keep in mind that for other date formats or more complex data, additional preprocessing or tailored commands may be required. It’s always important to ensure the date format is consistent across your data to facilitate accurate sorting.

What Can You Do Next πŸ™πŸ˜Šβ€‹

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle