跳转至

64. 脚本:单词及字母去重排序

12. 单词及字母去重排序(排序去重面试非常常见!!!)

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,by oldboy training.

思路分析

1.tr " ," "\n" <12.log |sort |uniq -c |sort -rn
2.tr " ," "\n" <12.log |awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn
3.xargs -n1 <12.log
[root@k8s-master tmp]# tr " ," "\n" <12.log |sort |uniq -c |sort -rn                         2 the
      2 support
      2 squid
      2 and
      1 users
      1 training.
      1 to
      1 sections
      1 resources
      1 provides
      1 project
      1 Please
      1 oldboy
      1 of
      1 number
      1 more
      1 installations.
      1 infomation
      1 implement
      1 for
      1 documentation
      1 design
      1 by
      1 browse
      1 assist
      1 a
[root@k8s-master tmp]# tr " ," "\n" <12.log |awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn
2 the
2 support
2 squid
2 and
1 users
1 training.
1 to
1 sections
1 resources
1 provides
1 project
1 Please
1 oldboy
1 of
1 number
1 more
1 installations.
1 infomation
1 implement
1 for
1 documentation
1 design
1 by
1 browse
1 assist
1 a
[root@k8s-master tmp]# xargs -n1 <12.log|sort|uniq -c |sort -rn
      2 the
      2 support
      2 squid
      2 and
      1 users
      1 training.
      1 to
      1 sections
      1 resources
      1 provides
      1 project
      1 Please
      1 oldboy
      1 of
      1 number
      1 more
      1 installations.
      1 infomation,by
      1 for
      1 documentation
      1 design,implement
      1 browse
      1 assist
      1 a
sed 's# ##g' 12.log
[root@k8s-master tmp]# sed 's# ##g' 12.log
thesquidprojectprovidesanumberofresourcestoassistusersdesign,implementandsupportsquidinstallations.Pleasebrowsethedocumentationandsupportsectionsformoreinfomation,byoldboytraining.
grep -o "." 12.log|sort|uniq -c |sort -rn
[root@k8s-master tmp]# grep -o "." 12.log|sort|uniq -c |sort -rn
     27
     19 s
     18 o
     17 e
     15 t
     14 n
     14 i
     12 r
     10 a
      8 u
      8 d
      7 p
      6 m
      5 l
      4 c
      4 b
      3 f
      2 y
      2 q
      2 h
      2 g
      2 .
      2 ,
      1 w
      1 v
      1 P
      1 j
grep -o "[^ ]" 12.log |sort | uniq -c |sort -rn
[root@k8s-master tmp]# grep -o "[^ ]" 12.log |sort | uniq -c |sort -rn
     19 s
     18 o
     17 e
     15 t
     14 n
     14 i
     12 r
     10 a
      8 u
      8 d
      7 p
      6 m
      5 l
      4 c
      4 b
      3 f
      2 y
      2 q
      2 h
      2 g
      2 .
      2 ,
      1 w
      1 v
      1 P
      1 j
grep -o "[^ ]" 12.log|awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn
[root@k8s-master tmp]# grep -o "[^ ]" 12.log|awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn
19 s
18 o
17 e
15 t
14 n
14 i
12 r
10 a
8 u
8 d
7 p
6 m
5 l
4 c
4 b
3 f
2 y
2 q
2 h
2 g
2 .
2 ,
1 w
1 v
1 P
1 j

最后更新: 2022-02-19 13:59:07