首页 » Linux » 20道必会shell题第20题

20道必会shell题第20题

 

企业面试题20(中企动力)::用shell处理以下内容
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!
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

解析:tr可以将多个参数替换成一个参数,
tr ” ,.” “\n” :也就是把有空格,逗号,句号的东西都换成加车。
第二个对字母进行排序,主要是要把字母分成一行一行的,可以用grep -o “\w”

答案:
[root@nfs_server scripts]# 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# #\n#g’ |sed ‘s#,#\n#g’|sort|uniq -c|sort -nr
2 support
2 squid
2 and
1 users
1 toassist
1 the
1 sections
1 resources
1 provides
1 project
1 Please
1 of
1 number
1 more
1 installations.
1 infomation
1 implement
1 for
1 documentation
1 design
1 browsethe
1 a

或:[root@nfs_server scripts]# 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”|tr ” ,” “\n”|sort|uniq -c|sort -rn|cat -n
1 2 support
2 2 squid
3 2 and
4 1 users
5 1 toassist
6 1 the
7 1 sections
8 1 resources
9 1 provides
10 1 project
11 1 Please
12 1 of
13 1 number
14 1 more
15 1 installations.
16 1 infomation
17 1 implement
18 1 for
19 1 documentation
20 1 design
21 1 browsethe
22 1 a

第二个答案:
[root@lamp_server scripts]# 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”|grep -o ‘\w’|sort|uniq -c|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

原文链接:20道必会shell题第20题,转载请注明来源!

0