{"id":11321,"date":"2019-03-13T00:50:45","date_gmt":"2019-03-13T00:50:45","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11321"},"modified":"2019-03-13T00:54:23","modified_gmt":"2019-03-13T00:54:23","slug":"14-useful-examples-of-linux-sort-command","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/14-useful-examples-of-linux-sort-command\/","title":{"rendered":"14 Useful Examples of Linux \u2018sort\u2019 Command"},"content":{"rendered":"<p>Sort is a Linux program used for printing lines of input text files and concatenation of all files in sorted order. Sort command takes blank space as field separator and entire Input file as sort key. It is important to notice that sort command don\u2019t actually sort the files but only print the sorted output, until your redirect the output.<\/p>\n<p>This article aims at deep insight of Linux \u2018<strong>sort<\/strong>\u2018 command with 14 useful practical examples that will show you how to use sort command in Linux.<\/p>\n<p><strong>1.<\/strong>\u00a0First we will be creating a text file (<strong>tecmint.txt<\/strong>) to execute \u2018<strong>sort<\/strong>\u2018 command examples. Our working directory is \u2018<strong>\/home\/$USER\/Desktop\/tecmint<\/strong>.<\/p>\n<p>The option \u2018<strong>-e<\/strong>\u2018 in the below command enables interpretion of backslash and\u00a0<strong>\/n<\/strong>\u00a0tells\u00a0<strong>echo<\/strong>\u00a0to write each string to a new line.<\/p>\n<pre>$ echo -e \"computer\\nmouse\\nLAPTOP\\ndata\\nRedHat\\nlaptop\\ndebian\\nlaptop\" &gt; tecmint.txt\r\n\r\n<\/pre>\n<p><center><\/center><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Split-String-by-Lines.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12572\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Split-String-by-Lines.gif\" alt=\"Split String by Lines in Linux\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p><strong>2.<\/strong>\u00a0Before we start with \u2018<strong>sort<\/strong>\u2018 lets have a look at the contents of the file and the way it look.<\/p>\n<pre>$ cat tecmint.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Check-Content-of-File.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12573\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Check-Content-of-File.gif\" alt=\"Check Content of File\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p><strong>3.<\/strong>\u00a0Now sort the content of the file using following command.<\/p>\n<pre>$ sort tecmint.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12574\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content.gif\" alt=\"Sort Content of File linux\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p><strong>Note:<\/strong>\u00a0The above command don\u2019t actually sort the contents of text file but only show the sorted output on terminal.<\/p>\n<p><strong>4.<\/strong>\u00a0Sort the contents of the file \u2018<strong>tecmint.txt<\/strong>\u2018 and write it to a file called (<strong>sorted.txt<\/strong>) and verify the content by using\u00a0<a title=\"Use cat Command to Check Content File\" href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>.<\/p>\n<pre>$ sort tecmint.txt &gt; sorted.txt\r\n$ cat sorted.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-File-Content.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12575\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-File-Content.gif\" alt=\"Sort File Content in Linux\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p><strong>5.<\/strong>\u00a0Now sort the contents of text file \u2018<strong>tecmint.txt<\/strong>\u2018 in reverse order by using \u2018<strong>-r<\/strong>\u2018 switch and redirect output to a file \u2018<strong>reversesorted.txt<\/strong>\u2018. Also check the content listing of the newly created file.<\/p>\n<pre>$ sort -r tecmint.txt &gt; reversesorted.txt\r\n$ cat reversesorted.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-By-Reverse.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12576\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-By-Reverse.gif\" alt=\"Sort Content By Reverse\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p><strong>6.<\/strong>\u00a0We are going a create a new file (<strong>lsl.txt<\/strong>) at the same location for detailed examples and populate it using the output of \u2018<strong>ls -l<\/strong>\u2018 for your home directory.<\/p>\n<pre>$ ls -l \/home\/$USER &gt; \/home\/$USER\/Desktop\/tecmint\/lsl.txt\r\n$ cat lsl.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Populate-Output.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12577\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Populate-Output.gif\" alt=\"Populate Output of Home Directory\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>Now will see examples to sort the contents on the basis of other field and not the default initial characters.<\/p>\n<p><strong>7.<\/strong>\u00a0Sort the contents of file \u2018<strong>lsl.txt<\/strong>\u2018 on the basis of\u00a0<strong>2nd column<\/strong>\u00a0(which represents number of symbolic links).<\/p>\n<pre>$ sort -nk2 lsl.txt\r\n<\/pre>\n<p><strong>Note:<\/strong>\u00a0The \u2018<strong>-n<\/strong>\u2018 option in the above example sort the contents numerically. Option \u2018<strong>-n<\/strong>\u2018 must be used when we wanted to sort a file on the basis of a column which contains numerical values.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-by-Column.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12578\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-by-Column.gif\" alt=\"Sort Content by Column\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p><strong>8.<\/strong>\u00a0Sort the contents of file \u2018<strong>lsl.txt<\/strong>\u2018 on the basis of\u00a0<strong>9th column<\/strong>\u00a0(which is the name of the files and folders and is non-numeric).<\/p>\n<pre>$ sort -k9 lsl.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-Based-on-Column.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12579\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-Based-on-Column.gif\" alt=\"Sort Content Based on Column\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p><strong>9.<\/strong>\u00a0It is not always essential to run sort command on a file. We can pipeline it directly on the terminal with actual command.<\/p>\n<pre>$ ls -l \/home\/$USER | sort -nk5\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-By-Pipeline.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12580\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-By-Pipeline.gif\" alt=\"Sort Content Using Pipe Option\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p><strong>10.<\/strong>\u00a0Sort and remove duplicates from the text file\u00a0<strong>tecmint.txt<\/strong>. Check if the duplicate has been removed or not.<\/p>\n<pre>$ cat tecmint.txt\r\n$ sort -u tecmint.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-and-Remove-Duplicates.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12585\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-and-Remove-Duplicates.gif\" alt=\"Sort and Remove Duplicates\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>Rules so far (what we have observed):<\/p>\n<ol>\n<li>Lines starting with numbers are preferred in the list and lies at the top until otherwise specified (<strong>-r<\/strong>).<\/li>\n<li>Lines starting with lowercase letters are preferred in the list and lies at the top until otherwise specified (<strong>-r<\/strong>).<\/li>\n<li>Contents are listed on the basis of occurrence of alphabets in dictionary until otherwise specified (<strong>-r<\/strong>).<\/li>\n<li>Sort command by default treat each line as string and then sort it depending upon dictionary occurrence of alphabets (Numeric preferred; see rule \u2013 1) until otherwise specified.<\/li>\n<\/ol>\n<p><strong>11.<\/strong>\u00a0Create a third file \u2018<strong>lsla.txt<\/strong>\u2018 at the current location and populate it with the output of \u2018<strong>ls -lA<\/strong>\u2018 command.<\/p>\n<pre>$ ls -lA \/home\/$USER &gt; \/home\/$USER\/Desktop\/tecmint\/lsla.txt\r\n$ cat lsla.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Populate-Output-With-Hidden-Files.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12581\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Populate-Output-With-Hidden-Files.gif\" alt=\"Populate Output With Hidden Files\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>Those having understanding of \u2018<strong>ls<\/strong>\u2018 command knows that \u2018<strong>ls -lA\u2019=\u2019ls -l<\/strong>\u2018 +\u00a0<strong>Hidden<\/strong>\u00a0files. So most of the contents on these two files would be same.<\/p>\n<p><strong>12.<\/strong>\u00a0Sort the contents of two files on standard output in one go.<\/p>\n<pre>$ sort lsl.txt lsla.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-of-Multiple-Files.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12582\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-of-Multiple-Files.gif\" alt=\"Sort Contents of Two Files\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>Notice the repetition of files and folders.<\/p>\n<p><strong>13.<\/strong>\u00a0Now we can see how to sort, merge and remove duplicates from these two files.<\/p>\n<pre>$ sort -u lsl.txt lsla.txt\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Merge-Duplicates-Content.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12583\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Merge-Duplicates-Content.gif\" alt=\" Sort, Merge and Remove Duplicates from File\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>Notice that duplicates has been omitted from the output. Also, you can write the output to a new file by redirecting the output to a file.<br \/>\n<strong><br \/>\n14.<\/strong>\u00a0We may also sort the contents of a file or the output based upon more than one column. Sort the output of \u2018<strong>ls -l<\/strong>\u2018 command on the basis of field 2,5 (Numeric) and 9 (Non-Numeric).<\/p>\n<pre>$ ls -l \/home\/$USER | sort -t \",\" -nk2,5 -k9\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-By-Field-Column.gif\"><img decoding=\"async\" class=\"size-medium wp-image-12584\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/04\/Sort-Content-By-Field-Column.gif\" alt=\"Sort Content By Field Column\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>That\u2019s all for now. In the next article we will cover a few more examples of \u2018<strong>sort<\/strong>\u2018 command in detail for you. Till then stay tuned and connected to Tecmint. Keep sharing. Keep commenting. Like and share us and help us get spread.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/sort-command-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sort is a Linux program used for printing lines of input text files and concatenation of all files in sorted order. Sort command takes blank space as field separator and entire Input file as sort key. It is important to notice that sort command don\u2019t actually sort the files but only print the sorted output, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/14-useful-examples-of-linux-sort-command\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;14 Useful Examples of Linux \u2018sort\u2019 Command&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11321","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11321"}],"version-history":[{"count":3,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11321\/revisions"}],"predecessor-version":[{"id":11325,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11321\/revisions\/11325"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}