OS Sort Command:- Sorting by character position

Background

When reading sample code and I see the following command


sort -k1,1 -n | cut -f2- -d' '

I be like hold up, wait a minute.

Data Files

Person.txt


john                smith               jsmith@nobody.com             los angeles         CA        90001
larry               summers             lsummers@treasury.gov         new york-city       NY        10001
twilla              jacobs              tjacobs@cincy.com             cincinnati          OH        45201
andy                johnson             ajohnson@yahoo.com            miami               FL        33101
tanya               wilson              twilson@gmail.com             philadephia         PA        19019


Image

Metadata

Column Position Column – Start & End
First Name 1 1, 20
Last Name 2 21, 40
Email Address 3 41, 70
City 4 71, 90
State 5 90, 100
Postal Code 6 101, 110

Sorting

Sort Command

OS Platform – Win OS

Parameters

Parameter Description
/+<N> Specifies the character position number where sort will begin each comparison. N can be any valid integer.

Syntax


sort /+<N> [column-begin]

Sample

Sample – Sort By Email Address
Command

sort /+41 c:\temp\person.txt

Image

Sample – Sort By City
Command

sort /+71 c:\temp\person.txt

Image

OS Platform – Linux

Parameters

Parameter Description Explanation
–debug annotate the part of the line used to sort, and warn about questionable usage to stderr
-k, –key=KEYDEF sort via a key; KEYDEF gives location and type KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line’s end

Syntax

Syntax


sort -k1.[column-begin],1.[column-end] [file]

Sample

Sample – Sort By Email Address
Command

sort -k1.41,1.70 /c/temp/person.txt

Image

Sample – Sort By City
Command

sort -k1.71,1.90 /c/temp/person.txt

Image

 

Sample – Sort By Postal Code
Command

sort -nk1.101,1.110 --debug /c/temp/person.txt

Image

 

Explanation
  1. Sorted by postal Code
    • -n
      • Numeric Data
    • -k
      • column position begin and end
        • Column position 101-110
    • –debug
      • Place an underline underneath the ordering column

Source Code

GitHub

Gist

Link


john smith jsmith@nobody.com los angeles CA 90001
larry summers lsummers@treasury.gov new york-city NY 10001
twilla jacobs tjacobs@cincy.com cincinnati OH 45201
andy johnson ajohnson@yahoo.com miami FL 33101
tanya wilson twilson@gmail.com philadephia PA 19019

view raw

person.txt

hosted with ❤ by GitHub


john smith jsmith@nobody.com los angeles CA 90001
larry summers lsummers@treasury.gov new york-city NY 10001
twilla jacobs tjacobs@cincy.com cincinnati OH 45201
andy johnson ajohnson@yahoo.com miami FL 33101
tanya wilson twilson@gmail.com philadephia PA 19019

References

MS Windows

Reference

  1. Microsoft
    • Microsoft | Docs
      • Docs / Windows Server / Windows Commands / Reference

Linux

Reference

  1. man7.org
    • Linux \ man-pages
      • sort(1) — Linux manual page
        Link

Sample

  1. Stack Overflow
    • Forcing a Linux Sort to Treat a Record With Spaces as a Single Data Column
      Link
  2.  GeeksforGeeks
    • SORT command in Linux/Unix with examples
      Link
  3. Unix.Com
    • Unix and Linux Forums
      • Rohon & Scott
        • Top Forums Shell Programming and Scripting sorting a fixed width seq file
          Link
      • jim mcnamara
        • Top Forums Shell Programming and Scripting sort by based on multiple columns
          Link

Summary

The Linux Sort command is supremely much more versatile than its MS Windows counterpart.

For instance, the Linux Sort command supports sorting by column number, whereas the corresponding MS Windows command only supports absolute specific column position.

 

Dedication

Dedicating to the WSL team at MSFT.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s