Use the shell to count the number of occurrences of a string and sort the display from large to small

Source: Internet
Author: User
Requirement Description: The string in the document is counted and sorted (from large to small), the test.txt content of the first 2 records is displayed as follows: A 12332a 2323a 234234b 232c 23432c 34 statistics and Sorting methods: [Email Protected] ~]$ awk ' {a[$1]++} END {for (I in a) {print i,a[i] | "Sort-r-K 2"} ' Test.txt > R.txt execution Result: A 3c 2b 1 the method of printing the first two records is as follows: [[email protected] ~]$ head-2 r.txt//Method 1 show first two lines a 3c 2[ [Email protected] ~]$ head-n 2 r.txt//Method 2 shows the first two lines a 3c 2 using the awk command: awk ' fnr<=2 ' r.txt//Method 3 shows the first two lines


Use the shell to count the number of occurrences of a string and sort the display from large to small

Related Article

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.