{"id":911,"date":"2016-05-06T19:10:09","date_gmt":"2016-05-06T19:10:09","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=911"},"modified":"2016-05-06T19:10:09","modified_gmt":"2016-05-06T19:10:09","slug":"command-line-fun-listing-and-sorting","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/05\/06\/command-line-fun-listing-and-sorting\/","title":{"rendered":"command line fun &#8211; listing and sorting"},"content":{"rendered":"<p>Linux is not Unix yet it is very similar. \u00a0They are so similar I never really notice the difference, even when <a href=\"http:\/\/blog.paranoidprofessor.com\/index.php\/2015\/11\/06\/command-line-fun-the-basics\/\" target=\"_blank\">writing scripts<\/a>. \u00a0I needed to get a list of sorted files, so I tried the usual commands.<\/p>\n<div class=\"sbody-code\">\n<pre><code>ls -lS<\/code><\/pre>\n<\/div>\n<p>However, there is a small difference for sorting files by size, well in Solaris. \u00a0I was able to come up with a solution, but it was a bit more convoluted.<\/p>\n<div class=\"sbody-code\">\n<pre><code>ls -lS | sort -n <\/code><\/pre>\n<\/div>\n<p>This actually worked just fine, despite how odd it looked to me. \u00a0However, the sort command can be used to do a lot more than just sorting file sizes.<\/p>\n<p>Most of the time I use the sort command for the simplest of uses. \u00a0Usually it is to take a list of values and produce a sorted list, usually to be used later as a form of input.<\/p>\n<div class=\"sbody-code\">\n<pre><code>ls -l | nawk '{print ($6 \" \" $7 \" \" $8)}' | sort\r\n\r\nMay 30 2012\r\nMay 30 2012\r\nMay 30 2012\r\nMay 30 2012\r\nMay 30 2012\r\nMay 30 2012\r\nMay 30 2012\r\nMay 30 2012\r\nMay 31 2012\r\nMay 31 2012\r\nMay 31 2012\r\nSep 27 2011\r\nSep 27 2011\r\nSep 27 2011\r\nSep 27 2011\r\nSep 30 2011\r\n<\/code><\/pre>\n<\/div>\n<p>It depends on the actual input, in this case the values don&#8217;t produce a unique list of dates. \u00a0It is possible to have some sort of control break logic to deal with changing data, but this is actually a bit heavy programming considering this is shell scripting. It is actually easier to filter out these values while sorting.<\/p>\n<p>This filtering can be done by passing the &#8220;-u&#8221; command to the sort command. \u00a0This causes the control break logic to be executed\u00a0by sort and have it remove the duplicates.<\/p>\n<div class=\"sbody-code\">\n<pre><code>ls -l | nawk '{print ($6 \" \" $7 \" \" $8)}' | sort -u\r\n  \r\nMay 30 2012\r\nMay 31 2012\r\nSep 27 2011\r\nSep 30 2011\r\n<\/code><\/pre>\n<\/div>\n<p>This works great depending on the type of input, but this example doesn&#8217;t work quite well with numeric values.<\/p>\n<div class=\"sbody-code\">\n<pre><code>ls -l | nawk '{print ($5 )}' | sort \r\n\r\n13752\r\n1423360\r\n2527\r\n2918400\r\n4096\r\n4096\r\n4096\r\n4096\r\n4096\r\n4096\r\n4096\r\n47\r\n55\r\n577\r\n616\r\n686080\r\n<\/code><\/pre>\n<\/div>\n<p>The sorted list doesn&#8217;t make all that much sense, unless you consider that the list has been sorted as alpha numeric data. \u00a0The values that begin with &#8216;1&#8217; are listed before those starting with &#8216;9&#8217; despite the entire number having a much different value.<\/p>\n<p>This problem was obviously foreseen as\u00a0it is possible to use the &#8220;-n&#8221; parameter to have the sorted values to be treated as numeric instead of alpha numeric.<\/p>\n<div class=\"sbody-code\">\n<pre><code>ls -l | nawk '{print ($5 )}' | sort -n \r\n\r\n47\r\n55\r\n577\r\n616\r\n2527\r\n4096\r\n4096\r\n4096\r\n4096\r\n4096\r\n4096\r\n4096\r\n13752\r\n686080\r\n1423360\r\n2918400\r\n<\/code><\/pre>\n<\/div>\n<p>This covers most of the situations that I have needed the sort command for, but this just scrapes the surface of how flexible this command is. \u00a0It is possible to sort not only simple lists but delimeted lists. \u00a0The sorting can be done for one or more columns and the values can be reversed if necessary.<\/p>\n<table class=\"w3-table-all\">\n<tbody>\n<tr>\n<th>sort argument<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>-u<\/td>\n<td>only unique values<\/td>\n<\/tr>\n<tr>\n<td>-n<\/td>\n<td>treat values as numeric<\/td>\n<\/tr>\n<tr>\n<td>-r<\/td>\n<td>sorted values in reverse order<\/td>\n<\/tr>\n<tr>\n<td>-t<\/td>\n<td>field delimeter<br \/>\nThis is used when the input contains multiple fields.<\/td>\n<\/tr>\n<tr>\n<td>-k&lt;#&gt;<\/td>\n<td>when multiple fields are part of the input,<br \/>\nthe field &#8220;#&#8221; will be the field that is sorted.It is possible to add multiple &#8220;-k&#8221; parameters to sort on multiple fields. \u00a0The fields will be sorted based on the order that they show up on the command line.(-k2 -k1 will sort first on field 2 within field 2 will sort on field 1)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Linux is not Unix yet it is very similar. \u00a0They are so similar I never really notice the difference, even when writing scripts. \u00a0I needed to get a list of sorted files, so I tried the usual commands. ls -lS &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/05\/06\/command-line-fun-listing-and-sorting\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/911"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=911"}],"version-history":[{"count":8,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/911\/revisions"}],"predecessor-version":[{"id":1970,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/911\/revisions\/1970"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}