2.sort Sort Command explanation

Source: Internet
Author: User

Sort command

Sort : Text sort, just the sort of display files, without affecting the order of source files, is based on assii codethe word Fu Shen order to arrange. -N: Installation values are arranged from small to large, ascending by default. - R: Descending order * * * *-T: Specifies the field delimiter, which means separating fields by dot (similar to Awk-f, take fields with the-D of $1,$2 or cut, take field F numbers)- K: Specifies the number of characters in the column or columns, equivalent to the-F2 in the cut-d "delimiter"-u: output only one line for the same line * * * *- F: Ignore character caseExample: Remove "A-Z" subtitles and sort
  1. [[email protected]-server test]# cat test.txt
  2. 10.0.0.9 a
  3. 10.0.0.8 k
  4. 10.0.0.7 f
  5. 10.0.0.7 n
  6. 10.0.0.8 c
  7. 10.0.0.8 z
  8. 10.0.0.9 o
Method 1:
  1. [[email protected]-server test]# cut -d " "-f2 test.txt|sort -n ##-d后面跟分隔符为空格,-f指定第2列,然后通过sort排序
  2. a
  3. c
  4. f
  5. k
  6. n
  7. o
  8. z
Method 2:
  1. [[email protected]-server test]# awk -F ‘ ‘‘{print $2}‘ test.txt|sort -n
  2. a
  3. c
  4. f
  5. k
  6. n
  7. o
  8. z
Method 3: The delimiter default is a space, so-t can be omitted.
  1. [[email protected]-server test]# sort -t " "-k2 test.txt
  2. 10.0.0.9 a
  3. 10.0.0.8 c
  4. 10.0.0.7 f
  5. 10.0.0.8 k
  6. 10.0.0.7 n
  7. 10.0.0.9 o
  8. 10.0.0.8 z
  9. [[email protected]-server test]#
Can be simplified to:
  1. [[email protected]-server test]# sort -k2 test.txt
  2. 10.0.0.9 a
  3. 10.0.0.8 c
  4. 10.0.0.7 f
  5. 10.0.0.8 k
  6. 10.0.0.7 n
  7. 10.0.0.9 o
  8. 10.0.0.8 z
Example 2: The implementation of the IP address, sorted in reverse order sorting method: 

2.sort Sort Command explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.