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
- Sorted by postal Code
- -n
- Numeric Data
- -k
- column position begin and end
- Column position 101-110
- column position begin and end
- –debug
- Place an underline underneath the ordering column
- -n
Source Code
GitHub
Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
- Microsoft
- Microsoft | Docs
- Docs / Windows Server / Windows Commands / Reference
- Sort
Link
- Sort
- Docs / Windows Server / Windows Commands / Reference
- Microsoft | Docs
Linux
Reference
- man7.org
- Linux \ man-pages
- sort(1) — Linux manual page
Link
- sort(1) — Linux manual page
- Linux \ man-pages
Sample
- Stack Overflow
- Forcing a Linux Sort to Treat a Record With Spaces as a Single Data Column
Link
- Forcing a Linux Sort to Treat a Record With Spaces as a Single Data Column
- GeeksforGeeks
- SORT command in Linux/Unix with examples
Link
- SORT command in Linux/Unix with examples
- Unix.Com
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.