The number of occurrences of a word in the shell statistics text

Source: Internet
Author: User

Ubuntu14.04

Given a text, count the number of occurrences of the word

# solution 1

grep is used in conjunction with AWK and is written as a sh script fre. SH

SH fre.sh wordfretest.txt
#! /bin/Bash
# solution 1if[$#-eq0 ] ThenEcho "usage:$0 args Error"Exit0fiif[$#-ge2 ] ThenEcho "analyse The first file"fi#get the firstfilefilename=$1grep-e-o"\b[[:alpha:]]+\b"$filename |awk '{count[$0]++}end{printf ("%-20s%s\n","Word","Count"); for(Wordinchcount) {printf ("%-20s%s\n", Word,count[word])}}'

###########################

First determine if the input is correct, if the input is greater than 1 files, use the first file

Use grep to extract the words, use awk to count the words, and finally print them out.

###########################

Additional notes:

Parameter description:

-eq: Equals
-ne: Not equal to
-le: Less than or equal to
-ge: greater than or equal to
-LT: Less than
-GT: Greater Than

\b Backspace printf parameters

awk description

AWK consists of content and action; awk Pattern {action}

Pattern can be a begin; END; Expression expression, expression;

Can execute for (var in array) statement

1.BEGIN module: This module includes an action block (the contents of "{}"). The action block is executed before the file is entered,

That is, you do not need to enter any file data, you can execute the module.

The begin module is often used to set changes to built-in variables such as (OFS,RS,FS, etc.), assign initial values to user-defined variables, or print header information.

The statement operations in the Begin module are separated by a ";" Flag or a branch.

Eg:awk ' Begin{print ' Hello world!   Begin doing! "}" #输出字符串

2. End module: Instead of the Begin module, the operation is done after the file has been processed. does not match any input rows and is often used to output some summary information.

To match an expression:

[[: Alpha:]] represents the letter
[[: Alnum:]] represents letters and numeric characters
[A-za-z0-9] Represents a single alphabetic and numeric character

Grep-e "\b[[:alpha:]]+\b" move.sh
Match all the words in the move.sh
GREP-E-O "\b[[:alpha:]]+\b" move.sh
Print the words that match to 1 per line
"\b[[:alpha:]]+\b"
To match the whole word.

Another method, assuming that Words.txt is the target file, uses only one line of code

# solution 2

Awk-f ' {for (i=1;i<=nf;i=i+1) {print $i}} ' Words.txt |sort|uniq-c|sort-nr|awk-f ' ' {printf ("%s%s\n", $2,$1)} '

The number of occurrences of a word in the shell statistics text

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.