企业Shell面试题18:单词及字母去重排序案例
生活随笔
收集整理的這篇文章主要介紹了
企业Shell面试题18:单词及字母去重排序案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、按單詞出現頻率降序排序!
2、按字母出現頻率降序排序!
the?squid?project?provides?a?number?of?resources?to?assist?users?design,implement?and?support?squid?installations.?Please?browse?the?documentation?and?support?sections?for?more?infomation
解答:
[root@oldboy?~]#?cat?oldboy.txt?
the?squid?project?provides?a?number?of?resources?to?assist?users?design,implement?and?support?squid?installations.?Please?browse?the?documentation?and?support?sections?for?more?infomation按單詞排序解答:法1:
[root@oldboy?scripts]#?tr?",."?"?"?<oldboy.log|xargs?-n?1|sort|uniq?-c|sort?-rn|head2?the2?support2?squid2?and1?users1?training1?to1?sections1?resources1?provides法2:
[root@oldboy?scripts]#?tr?",."?"?"?<oldboy.log|xargs?-n?1|awk?'{S[$1]++}END{for(key?in?S)print?S[key],key}'|sort?-rn|head2?the
2?support
2?squid
2?and
1?users
1?training
1?to
1?sections
1?resources
1?provides法3:
[root@oldboy?scripts]#?awk?-F?"[,.?]+"?'{for(i=1;i<=NF;i++)S[$i]++}END{for(key?in?S)print?S[key],key}'?oldboy.log?|sort?-rn|head
2?the
2?support
2?squid
2?and
1?users
1?training
1?to
1?sections
1?resources
1?provides按字母頻率排序
法1
[root@MySQL?~]#?tr?"{?|,|.}"?"\n"<oldboy.txt|awk?-F?""??'{for(i=1;i<=NF;i++)array[$i]++}END{for(key?in?array)print?array[key],key|"sort?-nr"}'
19?s
17?e
16?o
14?t
12?n
12?i
11?r
9?a
8?u
7?p
7?d
6?m
4?l
4?c
3?f
2?q
2?h
2?b
1?w
1?v
1?P
1?j
1?g[root@MySQL?~]#?tr?"[?,.]"?"\n"<oldboy.txt|awk?'{for(i=1;?i<=length($0);?i++)?++S[substr($0,i,1)]}?END?{for(a?in?S)?print?S[a],?a|"sort?-rn"}'
19?s
17?e
16?o
14?t
12?n
12?i
11?r
9?a
8?u
7?p
7?d
6?m
4?l
4?c
3?f
2?q
2?h
2?b
1?w
1?v
1?P
1?j
1?g[root@db02?oldboy20151227]#?echo?"the?squid?project?provides?a?number?of?resources?toassist?users?design,implement?and?support?squid?installations.?Please?browsethe?documentation?and?support?sections?for?more?infomation"|sed?'s#?##g'|sed?-r?'s#(.)#\1\n#g'|sort|uniq?-c|sort?-rn?-k119?s17?e16?o14?t12?n12?i11?r9?a8?u7?p7?d6?m4?l4?c3?f2?q2?h2?b1?w1?v1?j1?g1?P1?.1?,1?[root@db02?oldboy20151227]#?echo?"the?squid?project?provides?a?number?of?resources?toassist?users?design,implement?and?support?squid?installations.?Please?browsethe?documentation?and?support?sections?for?more?infomation"|sed?'s#?##g'|awk?-F?""?'{for(n=1;n<=NF;n++)?print?$n}'|sort|uniq?-c|sort?-k1?-nr19?s17?e16?o14?t12?n12?i11?r9?a8?u7?p7?d6?m4?l4?c3?f2?q2?h2?b1?w1?v1?j1?g1?P1?.1?,
有關AWK數組的超詳細案例講解就在老男孩教育最新高級運維班課程!
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的企业Shell面试题18:单词及字母去重排序案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++构造函数详解(转)
- 下一篇: 最重要的7个Drupal内核模板文件