If we want to check data in text file is sorted or not, then we can use -c or –check, –check=diagnose-first option. Check if Content is Already Sorted using -c option If we will give, ja for January or au for August, then sort command would not consider it as month name. Note, lines in file should contain at least 3 character name of month name at start of line (e.g. The following sort command sorts lines in test file as per month order. If we want to sort in the order of months of year, then we can use -M or –month-sort option. The following sort command sorts human readable numbers (i.e 1K = 1 Thousand, 1M = 1 Million, 1G = 1 Giga, 1T = 1 Tera) in test file and displays sorted output. If we want to sort on human readable numbers (e.g., 2K 1M 1G), then we can use -h or –human-numeric-sort option. Sort Human Readable Numbers using -h option The following sort command sorts lines in test file on numeric value in first word of line and displays sorted output. If we want to sort on numeric value, then we can use -n or –numeric-sort option.Ĭreate the following test file for this example: $ cat test
It sorts lines in test file and displays sorted output. Based on your requirement, sort provides several command line options for sorting data in a text file.įor example, here is a test file: $ cat testĪnd, here is what you get when sort command is executed on this file without any option. You can sort the data in text file and display the output on the screen, or redirect it to a file. Sort command is helpful to sort/order lines in text files.