Count word frequency in a specific file

Source: Internet
Author: User
Tags egrep
It is interesting to count the frequency of words used in a specific file to find out the word frequency. Next, we use the associated array, awk, sed, grep and other methods to solve the problem. First, you need to save the name as word.txt for a test as follows: [p... it is interesting to count the frequency of words used in a specific file to find out the word frequency. Next, we use the associated array, awk, sed, grep and other methods to solve the problem. First, we need a text for testing. save the content named word.txt as follows: [python] Word used this counting this next we need to write a Shell script program, as shown below: [python] #! /Bin/bash # Name: word_freq.sh # Description: Find out frequency of words in a file if [$ #-ne 1]; then echo "Usage: $0 filename "; exit-1 fi filename = $1 egrep-o "\ B [[: alpha:] + \ B "$ filename | \ awk '{count [$0] ++} END {printf (" %-14 s % s \ n "," Word ", "Count"); \ for (ind in count) {printf ("%-14 s % d \ n", ind, count [ind]);} 'Working principle: 1. egrep-o "\ B [[: alpha:] + \ B" $ filename is used to output only words, and the-o option is used to print matching character sequences separated by line breaks, In this way, a word 2. \ B is a word boundary identifier in each line. [: Alpha:] is a character class that represents letters. 3. the awk command is used to avoid running each word following an iteration:
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.