Shell exercises count the number of words in descending order

Source: Internet
Author: User

Original: https://leetcode.com/problems/word-frequency/Write a bash script to calculate the frequency of each wordinchA textfilewords.txt.For simplicity sake, you could assume:words.txt contains only lowercase characters and space' 'characters. Each of the word must consist of lowercase characters only. Words is separated by one or Morewhitespace characters. For example, assume that words.txt have the following content:the day is sunny the thethe sunny are isyour script should out Put the following, sorted by descending frequency:the4 is3Sunny2 Day1

Simple point is to give you a file, inside is a space "can have multiple" divided word "all lowercase", please count the number of different words, and output in descending order.

The words are recorded in the array, then the output is traversed, the sort is sorted, and the 2nd column is numbered in descending order.

Answer:

awk ' {for (i=1;i<=nf;i++) num[$i]++;} end{for (k in num) print K "" num[k]}'  words.txt|  Sort2

Use sort sorting to specify which column uses-K, followed by numbers, sorted by numbers, use-N, and use-R in descending order.

Shell exercises count the number of words in descending order

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.