Linux Shell Programming Combat---to count the word frequency of words in a particular file

Source: Internet
Author: User

Method 1 : Using sed

Shell>cat A1.txt

123a123,555

456.333

566 . 555!88,thisis a good boy.


Shell>cat a1.txt|sed ' s/[[:space:]|[:p unct:]]/\n/g ' |sed '/^$/d ' |sort|uniq-c|sort-n-k1-r

2 555

1 This

1 is

1 good

1 boy

1 A123

1 A

1 88

1 566

1 456

1 333

1 123

Shell>


Sed ' s/[[:space:]|[:p unct:]]/\n/g '

[] represents a collection of regular expressions,[: space:] represents a space. [:p unct:] represents punctuation.

[[: space:]|[:p UNCT:]] represents a matching space or punctuation

s/[[:space:]|[:p unct:]]/\n/g represents the substitution of spaces or punctuation \ n line Break


sed '/^$/d ' Delete empty lines



Method 2 : Using awk

#!/bin/bash


Filename=$1


Cat$filename|awk ' {

#getline var;

Split ($0,a,/[[:space:]|[:p unct:]]/);

For (i in a) {

Word=a[i];

b[word]++;

}

}

end{

printf ("%-14s%s\n", "Word", "Count");

For (i in B) {

printf ("%-14s%d\n", I,b[i]) | " Sort-r-n-k2 ";

}


}

Run results

[Email protected]]# cat A1.txt

123a123,555

456.333

566 . 555!88,thisis a good boy.


[[Email protected]]#./word_freq.sh A1.txt

Word Count

555 2

This 1

is 1

Good 1

Boy 1

A123 1

A 1

88 1

566 1

456 1

333 1

123 1

1

[[Email protected]]#



Method 3 : Using TR

[Email protected]t01awk]# cat A1.txt

123a123,555

456.333

566i555!88,this is a good boy.


[Email protected]]# cat A1.txt |tr ' [: space:]| [:p unct:] ' \ n ' |tr-s ' \ n ' |sort|uniq-c|sort-n-k1-r

2 555

1 This

1 is

1 good

1 boy

1 A123

1 A

1 88

1 566i

1 456

1 333

1 123

[[Email protected]]#







This article is from the "operation and maintenance of micro-letter" blog, please be sure to keep this source http://weixiaoxin.blog.51cto.com/13270051/1963641

Linux Shell Programming Combat---to count the word frequency of words in a particular file

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.