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