sort command sorts the content in descending order by default. Sort this file was per the usernames in reverse alphabetic order. The /etc/passwd file contains the list of all users. The numerals represent salary of employees. Practice Questions on How to use sort command in Linux? 1 Answer Sorted by: 25 sort -V to the rescue: sort -V file From man sort: -V, -version-sort natural sort of (version) numbers within text In case you do not have the -V option in your sort command, there is an alternative: sort by first column starting on 4th character ( -k1.4) and then sort numerically ( -n ). For example, in Figure 7, the field separator is ‘ ’ and the data is sorted based on the second column Use of sort -t But, in case the data is in the form of columns like a database file or a comma separated file and you want to sort the data according to a particular column then we specify the column using -k option and use -t option to specify the field separator. Use of sort -r -t: field separatorīy default, sort command will sort the data by comparing the first character and move to the next character when the first character is same and so on. Use this option to sort the data in reverse order. Figure 5 shows this in comparison to the normal sort. If you have data based on the month in your file and you want to get the analysis according to months then -M will help to sort the data month-wise. i.e., how we sort numbers in general Use of sort -n -M: month sort Whereas, with -n option the output is as expected. Without the -n option the output is 115, 12, 15 … i.e., all numbers which start with 1 and then all numbers that start with 2 and so on. But to sort numbers as we do in our daily routine we need to use -n option. When you sort numbers, they are not sorted numerically by default which means by default first those numbers will be listed which start with 1, then with 2 and so on. The figure below shows the use of -f option where ‘Sort’ with capital ‘S’ appears first in the output. This option sorts the uppercase alphabets higher in order. Syntax of sort command $sort … …Įxample Use of sort command Options used with sort command -f: ignore case The sort command in Linux, as the name suggests, is used to sort the contents of a text file in the specified order. The sorted output is displayed on the screen. We use the sort command in Linux to sort the content of a file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |