Linux system sort command operation example

  
                

In the Linux command, the sort command can be used to sort the file lines. Many people don't know the sort command very well. The following small series will give a brief introduction to the example of the sort command. Friends who are not familiar with the sort command can understand under.

sort command is regarded as a plurality of domains each line of text, by default, in a space between domain or a series of spaces delimiter, the delimiter may further specify the -t option. Sort left to right by the ASCII value of the character (not the value).

1, remove duplicates: remove the same line in the file

sort -u filename

2, reverse ordering

sort –r filename

3, use k option to classify

The format of the command is:

sort -k [ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ][ Modifier ] ]

FStart.CStart, where FStart is the field used, and CStart means that the first character in the FStart field is “sorted first character”, similarly FEnd .Cend. However, if Cstart is omitted, it is the domain header, Cend is omitted, it is the domain tail, and if FEnd is omitted, it is the trailing edge. Modifier has the following important things: n, interpreting text as a numerical value; r, sorting in reverse order; f, ignoring case. Of course you can specify two k options, which will be parsed by rule. For example:

sort -n -k 2.2,3.1 -k 1,1r facebook.txt

The above is the way Linux uses the sort command, the sort command can remove the same line in the file. Sorting in reverse order, using k option classification, of course, there are many usages of the sort command, you need to use it flexibly.

Copyright © Windows knowledge All Rights Reserved