將一段(英文)按照字母出現的頻率進行倒序排列

1、文章

Newly-added concrete barriers and steel posts can be seen alongside
the bike path, and more will be installed in the coming months across the city, as the NYPD has promised.

2、操作過程:

2.1 將文中的換行符替換成空格
tr '\n' ' ' <a.txt
2.2 將空格去除
sed 's# ##g'
2.3 將單詞拆分成單個字母並換行
grep -o '\w'
2.4 單詞出現的頻率進行倒序
sort|uniq -c|sort -rn

3、最後的執行語句爲:(此語句是將區分大小寫的)
tr '\n' ' ' <a.txt|sed 's# ##g'|grep -o '\w' |sort|uniq -c |sort -rn

20 e
13 s
11 t
11 a
10 n
9 i
8 o
8 d
7 r
7 l
7 h
6 c
4 m
4 b
3 p
2 y
2 w
2 N
2 g
1 Y
1 P
1 k
1 D

4、tr '\n' ' ' <a.txt|awk '{print toupper($0)}'|sed 's# ##g'|grep -o '\w' |sort|uniq -c |sort -rn

 20 e
 13 s
 12 n
 11 t
 11 a
  9 i
  9 d
  8 o
  7 r
  7 l
  7 h
  6 c
  4 p
  4 m
  4 b
  3 y
  2 w
  2 g
  1 k

5、大小寫轉換:

awk '{print toupper($0)}' <a.txt (轉化爲大寫)
awk '{print tolower($0)}' <a.txt (轉化爲小寫)

=================================================================
按照單詞出現的頻率進行排序
sed 's#[,.]##g'<a.txt |tr ' ' '\n'|sort|uniq -c|sort -rn

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章