{"id":4244,"date":"2018-05-31T18:59:13","date_gmt":"2018-05-31T14:59:13","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-sort-command\/"},"modified":"2018-05-31T18:59:13","modified_gmt":"2018-05-31T14:59:13","slug":"linux-sort-command-tutorial-for-beginners-8-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-sort-command-tutorial-for-beginners-8-examples\/","title":{"rendered":"Linux sort Command Tutorial for Beginners (8 Examples)"},"content":{"rendered":"<p>Looking for a command line utility to sort content in text files? Look no further than <strong>Sort<\/strong>, a tool specifically built for this purpose. In this tutorial, we will discuss this command using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on Ubuntu 16.04 LTS machine.<\/p>\n<h2 id=\"linux-sort-command\">Linux Sort command<\/h2>\n<p>The Sort command allows you to sort lines in a text file. Following is its syntax:<\/p>\n<p class=\"command\">sort [OPTION]&#8230; [FILE]&#8230;<\/p>\n<p>And here&#8217;s how the tool&#8217;s man page describes it:<\/p>\n<pre>Write sorted concatenation of all FILE(s) to standard output.<br\/>With no FILE, or when FILE is -, read standard input.<\/pre>\n<p>Following are some Q&amp;A styled examples that should give you a good idea on how sort works.<\/p>\n<h2 id=\"q-how-to-use-sort-command\">Q1. How to use sort command?<\/h2>\n<p>Suppose you have a file that contains some names, and you want to sort those in alphabetical order. Then all you need to do is to pass the name of the file as input to the sort command.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">sort file1<\/p>\n<p>So if file 1 contained the following lines:<\/p>\n<pre>Zimbabwe<br\/>Serbia<br\/>Norway<br\/>Australia<\/pre>\n<p>Then the output would be:<\/p>\n<pre>Australia<br\/>Norway<br\/>Serbia<br\/>Zimbabwe<\/pre>\n<h2 id=\"q-how-to-make-sort-ignore-leading-blanks\">Q2. How to make sort ignore leading blanks?<\/h2>\n<p>Depending upon your locale, you may see sort producing unexpected results when lines contain leading blanks. For example:<\/p>\n<p>Suppose file contains following lines:<\/p>\n<pre>Zimbabwe<br\/>\u00a0Serbia<br\/>\u00a0 Norway<br\/>Australia<\/pre>\n<p>And you run sort, only to see the following result:<\/p>\n<pre>\u00a0 Norway<br\/>\u00a0Serbia<br\/>Australia<br\/>Zimbabwe<\/pre>\n<p>This may look unexpected, but what actually happened is that lines that contain leading blanks were sorted on the basis of blanks, while others were sorted alphabetically. To make sure the Sort command ignores leading blanks, use the -b option. So in that case, you&#8217;ll get the following result:<\/p>\n<pre>Australia<br\/>\u00a0 Norway<br\/>\u00a0Serbia<br\/>Zimbabwe<\/pre>\n<h2 id=\"q-how-to-make-sort-ignore-case\">Q3. How to make sort ignore case?<\/h2>\n<p>If a file has words\/lines beginning with both upper case and lower case characters, then sort displays those with upper case at top. However, if you want, you can change this behavior using the -f command line option.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">sort -f file1<\/p>\n<h2 id=\"q-how-to-make-sort-compare-numbers\">Q4. How to make sort compare numbers?<\/h2>\n<p>Suppose a file only contains numbers, and you want sort to order them. Then this can be made possible using the -g command line option.<\/p>\n<pre>sort -g file1<\/pre>\n<p>For example a file with following contents:<\/p>\n<pre>32000<br\/>2500<br\/>50000<br\/>54<\/pre>\n<p>Can be sorted using the sort command to produce following results:<\/p>\n<pre>54<br\/>2500<br\/>32000<br\/>50000<\/pre>\n<h2 id=\"q-how-to-make-sort-work-with-human-readable-numeric-values\">Q5. How to make sort work with human readable numeric values?<\/h2>\n<p>In case you want sort to work with human readable numeric values like 1K, 2G, etc, use the -h command line option.<\/p>\n<p class=\"command\">sort -h file1<\/p>\n<p>So, for example, a file with following lines:<\/p>\n<pre>1M<br\/>2G<br\/>3K<\/pre>\n<p>Can be sorted in the following way using the -h option:<\/p>\n<pre>3K<br\/>1M<br\/>2G<\/pre>\n<h2 id=\"q-how-to-make-sort-only-check-for-sorted-input\">Q6. How to make sort only check for sorted input?<\/h2>\n<p>Just in case you want sort to only check if a file is sorted or not, use the -c command line option.<\/p>\n<p class=\"command\">sort -c file1<\/p>\n<p>For example, if file1 contains the following lines:<\/p>\n<pre>dhg<br\/>lkh<br\/>zyb<br\/>abd<\/pre>\n<p>Then using -c will see sort producing the following output:<\/p>\n<pre>sort: file1:4: disorder: abd<\/pre>\n<p>So you observe that the tool not only points there&#8217;s a disorder but outputs its location as well.<\/p>\n<h2 id=\"q-how-to-make-sort-merge-already-sorted-files\">Q7. How to make sort merge already sorted files?<\/h2>\n<p>If you want sort to merge two already sorted files, then use the -m command line option.<\/p>\n<p class=\"command\">sort -m file1 file2<\/p>\n<p>For example, both file1 and file2 contained following lines in my case:<\/p>\n<pre>abd<br\/>dhg<br\/>lkh<br\/>zyb<\/pre>\n<p>And here&#8217;s how the -m option merged these files:<\/p>\n<pre>abd<br\/>abd<br\/>dhg<br\/>dhg<br\/>lkh<br\/>lkh<br\/>zyb<br\/>zyb<\/pre>\n<h2 id=\"q-how-to-make-sort-write-the-result-to-a-file\">Q8. How to make sort write the result to a file?<\/h2>\n<p>By default, the sort command writes output to STDOUT. However, you can force it to write to a given file using the -o option.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">sort file1 -o output.txt<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>The Sort command provides a lot of options. We have discussed few key ones here. We suggest you practice these first, and once you are done, head to the command&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/sort\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about it.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-sort-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-sort-command-tutorial-for-beginners-8-examples.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-sort-command%2F&amp;text=Linux+sort+Command+Tutorial+for+Beginners+%288+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-sort-command-tutorial-for-beginners-8-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-sort-command-tutorial-for-beginners-8-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-sort-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-sort-command-tutorial-for-beginners-8-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Looking for a command line utility to sort content in text files? Look no further than Sort, a tool specifically built for this purpose. In this tutorial, we will discuss this command using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on Ubuntu &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-4244","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=4244"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4244\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=4244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=4244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=4244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}