9/13/2023 0 Comments Sort dcommand in linuxsorting file on the basis of 2nd field, numerically:ġ2. sorting file on the basis of the 2nd field:ġ1. Note: For a file which has fields delimited by a space or a tab, there is no need to specify the "-t" option since the white space is the delimiter by default in sort.ġ0. Similarly, if the sorting is to be done on the basis of first 3 fields, it will be: '-k 1,3'. In our case, since the sorting is on the 1st field alone, we speciy '1,1'. In other words, sort can be used to sort on a range of fields just like how the group by in sql does. The format of '-k' is : '-km,n' where m is the starting key and n is the ending key. '-k' is used to specify the keys on the basis of which the sorting has to be done. '-t' option is used to provide the delimiter in case of files with delimiter. sorting a file containing multiple fields:Īs shown above, the file got sorted on the 1st field, by default. Let us consider a file with multiple fields:Ĩ. With this, the output is now sorted, merged and without duplicate records.įiles with multiple fields and delimiter: u option becomes more handy in case of multiple files. The result of sort with multiple files will be a sorted and merged output of the multiple files. Let us consider examples with multiple files, say file1 and file2, containing numbers: n option can sort the decimal numbers as well.ĥ. However, it is incorrect to sort a numerical file in this way since the sorting logic is incorrect. If the file had not contained '200', the default sort would have given proper result. In the above result, 200 got placed immediately below 20, not at the end which is incorrect. The default sort 'might' give incorrect result on a file containing numbers: Even if the file have had 10 'Linux' records, with -u option, only the first record is retained.ģ. '-u' option removes all the duplicate records in the file. The duplicate 'Linux' record got removed. sort removes the duplicates using the -u option: sort simply sorts the file in alphabetical order:Ģ. Let us consider a file with the following contents:ġ. Using options in sort command, it can also be used to sort numerically. By default, the sort command sorts file assuming the contents are ascii. Sort command is used to sort a file, arranging the records in a particular order.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |