OS Sort Command:- Sorting by column position

Background

Let us sort by column position.

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
First Name 1
Last Name 2
Email Address 3
City 4
State 5
Postal Code 6

Display Text File

Cat Command

OS Platform – Win OS

Not Supported

OS Platform – Linux

Parameters

Parameter Description
T Show hidden characters

Syntax


cat -T [file]

Sample

Sample – Cat with -T option
Command

cat -T /c/temp/person_delimited_tab.txt

Output – Image

Explanation
  1. Character :- ^I
    • Indicates the tab character

Sort Data File By Column Number

Sort Command

OS Platform – Win OS

Not Supported

OS Platform – Linux

Parameters

Parameter Description Explanation
–debug annotate the part of the line used to sort, and warn about questionable usage to stderr
-b Ignore leading blanks
-k Specify position
-t Delimiter character -t$’\t’ => Tab Character
-h human-numeric-sort compare human readable numbers (e.g., 2K 1G)
-n Numeric Sort compare according to string numerical value

Syntax


sort -t [delimiter] -k [column-position]  [file]

Sample

Sample – Sort By Email Address
Command

sort -t$'\t' -b -k 3 /c/temp/person_delimited_tab.txt

Image

Sample – Sort By City
Command

sort -t$'\t' -b -k 4 /c/temp/person_delimited_tab.txt

Image

 

Sample – Sort By Postal Code
Command

sort -t$'\t' -k 6 -n --debug /c/temp/person_delimited_tab.txt

Image

 

Explanation
  1. Sorted by postal Code
    • -t
      • $’\t’
      • Delimited by tab
    • -k
      • column position
        • Column position 6
    • -n
      • Numeric Data
    • –debug
      • Place an underline underneath the ordering column

Source Code

GitHub

Gist

file-person_delimited_tab-txt

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

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
    • Sorting data based on second column of a file
      Link

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 )

Facebook photo

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

Connecting to %s