Developer's Diary
Software development, with Terry Ebdon
03-Aug-2020 CLI sorting

Sorting from the command line

I needed to add a sort step into a Windows command script running on a client system.

The input is a log file that had been pre-filtered by awk, and could be treated as space delimited. The required sort order was ascending by username (field 4) then timestamp (field 1). The safest way to do this is with a stable sort using field 4 as a single key. The stable sort preserves the logged order, within user, even if two log entries have the same timestamp.

Sort limitations

Windows sort

The Windows utility seems very limited when compared to sort tools I've used in the past. Though it's interesting that this is the first time the command's simplicity has caused me a problem.

Limitations

  1. It can only sort text files
  2. A single key
  3. You can only give the key position, not its length
  4. Key is specified only by character position
  5. Sort is case insensitive (according to Microsoft, others disagree)
  6. No stable-sort option

Point 1 isn't an issue here, but the key limitations, in points 2 and 3, were. I'd also be concerned that there's no option for a stable sort.

I could have worked around this by modifying the awk script, that generates the input file, to include a key field at the start of each line. I didn't consider that as I wanted to avoid changes to working code on a live system.

GNU & POSIX sorts

A POSIX compatible sort with a stable-sort extension, e.g. GNU sort, is significantly better, though it still has surprising limitations.

  1. Can only sort text files
  2. Sort keys are based on field numbers

02-AUG-2020 👈 Top of page 👉 04-AUG-2020

Buy Me a Coffee at ko-fi.com
© 2020 Terry Ebdon.

Find me coding on GitHub, networking on LinkedIn, answering questions on Stack Exchange and hanging out on twitter.