Linux Sort Command With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-sort-command-examples/
PINTEREST
PINTEREST
INSTAGRAM

Linux provides a lot of useful tools for system administrators. These tools gives the administrator a developer power so the administrator can easily change, modify, filter, sort the data. sort is an other little but powerful tool. In this tutorial we will look different usage examples of sort. We will use following simple example file named fruits.txt

Syntax

Simply Sort

Sort can be used with file or standard input. In this example we will sort file content according to each lines start. As we see our file content is sorted alphabetically.

Simply Sort
Simply Sort

Descending Sort

By default sort is done ascending. Some time it can be useful to sort reverse or descending mode. This can be done like below by providing -r parameter

Descending Sort
Descending Sort

Add Output To File

After sort operation the new sorted content is printed to the standard output. One of the alternative way to save output is writing to a file.

Add Output To File
Add Output To File

Ignore Case

Sort is by default case sensitive operation which means while sorting character case upper/lower is detected and sorted accordingly. If this is not suits you it can be disabled with -f or --ignore-case parameters.

LEARN MORE  How To Enable BitTorrent Ports In Linux Firewall?

Check Input File If Sorted

Another useful feature of sort is check the status of files if it is already sorted. If it is already sorted there will be no output but if it is not sorted single line information about disorder will be given.

Check Input File If Sorted
Check Input File If Sorted

Sort Multiple Files

Sometimes data that will be sorted do not resides in a single file. Sort command do not have a native feature to use multiple files. Here other bash tools provides solution to merge multiple files into a single standard input stream. In this example we will cat multiple files and redirect to the standard input of sort.

Sort Multiple Files
Sort Multiple Files

Sort By Given Column

Up to now we have sorted our data from starting first character of the line. Our data may be in a structured manner with columns and we may want to sort by using different columns others than first column. This can be done with other bash tools but sort provides a simple way to do this.

Sort By Column
Sort By Column

Sort  By Multiple Columns

Sort by column is easily implemented but what will happen if we want to sort according to multiple columns. Here -k parameter can be used again with multiple values like below. We will sort primary with column 1 and then column 3

Sort By Multiple Columns
Sort By Multiple Columns

Sort Numbers

Up to now sorting operation is done with alphabet and ASCII. Detecting numbers and sorting accordingly can be done with sort. For example in normal sort operation 1 is lower than 10 but if we want to sort according numeric values -n parameter can be used like below.

LEARN MORE  Linux watch Command Usage With Examples

Sort Numbers
Sort Numbers

Sort By Size

While using Linux tools size information can be expressed in different way default expression method is prefixing with metric name like 1GB, 128MB etc. Sort supports this type of expression which is very good for simple sort implementations.

Sort By Size
Sort By Size

Sort By Date

Unix and Linux months are expressed like JAN , JUN etc. Sort can support this type of month expression and sorting them. The moths are listen column number 4 and we will sort according to column 4 as month value with the -M parameter

Sort By Date
Sort By Date
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-sort-command-examples/
PINTEREST
PINTEREST
INSTAGRAM

One Reply to “Linux Sort Command With Examples”

  1. Pingback: URL

Leave a Reply

Your email address will not be published.