Linux Jobs (iii)-shell statistics The most frequently occurring n words in an article and sorts the number of output occurrences

Source: Internet
Author: User
Tags processing text sorts

Linux classes on the assignment Wednesday, if there is a poor consideration to the place, please do not hesitate to enlighten.

Shell Processing Text See this blog for related frequently used commands

#

#假设输入两个參数 is the first to count the number of words. The second article to be counted

#假设输入一个參数 the default count of words is 10

#

Detailed ideas:

Replacing various symbols with newline (tr command)

Uppercase to lowercase (tr command)

Sort, count the number of words and remove repetition (sort and uniq)

Sort by the high and low number of occurrences (sort)

Print n A word that needs to be counted

#!/bin/bashif [$#-ne 2-a $#-ne 1]; then        echo "Usage: ' basename $ ' [n] Input file"        echo        exitfiif [$#- EQ 1];then        i_top=10        i_file=$1fiif [$#-eq 2];then        i_top=$1        i_file=$2fitr-sc "[A-z][a-z]" "  [\012 *] "  < $I _file |  TR  "[A-z]"  "[A-z]"  | sort  | uniq-c |   Sort  -k1-n-R  |  head-$I _top | nl


Linux Jobs (iii)-shell statistics The most frequently occurring n words in an article and sorts the number of output occurrences

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.