企业Shell面试题18:单词及字母去重排序案例

wKiom1XW4BagvgPxAAC_N_Bdskw467.jpg

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|head
      2 the
      2 support
      2 squid
      2 and
      1 users
      1 training
      1 to
      1 sections
      1 resources
      1 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 -k1
     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 j
      1 g
      1 P
      1 .
      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 -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 j
      1 g
      1 P
      1 .
      1 ,

有关AWK数组的超详细案例讲解就在老男孩教育最新高级运维班课程!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值