用shell处理以下内容 1、按单词出现频率降序排序! 2、按字母出现频率降序排序! the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support
排序的字符串如下: the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support
此题目有多种解法,sed、awk、tr等等,都可以解决此题,命令运用灵活多变。
编写shell脚本
解法1:
#!/bin/bash ###-------------CopyRight-------------
# Name:sort string
# Version Number:1.0
# Type:sh
# Language:bash shell
# Date:--
# Author:sandy
# QQ:
# Email:eeexu123@.com str="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"
#no1按单词出现的频率降序排序
word(){
echo $str|sed 's#[^a-zA-Z]#\n#g'|grep -v "^$"|sort|uniq -c|sort -rn -k1
} #no2按字母出现的频率降序排序
string(){
echo $str|grep -o "."|egrep -v "[^a-zA-Z]"|sort|uniq -c|sort -rn -k1
} menu(){
cat <<END
.按单词出现的频率降序排序
.按字母出现的频率降序排序
END
read -p "Pls you choose num:" num
}
menu usage(){
echo "USAGE:You muset choose 1 or 2"
exit
} case "$num" in
)
word
;;
)
string
;;
*)
usage
esac
解法2:
#!/bin/bash ##-------------CopyRight-------------
# Name:sort string
# Version Number:1.1
# Type:sh
# Language:bash shell
# Date:--
# Author:sandy
# QQ:
# Email:eeexu123@.com str="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"
#no1按单词出现的频率降序排序
word(){
echo $str|tr ' ' '\n'|sort|uniq -c|sort -rn -k1
} #no2按字母出现的频率降序排序
string(){
echo $str|sed -r 's#(.)#\1\n#g'|egrep -v "[^a-zA-Z]|^$"|sort|uniq -c|sort -rn -k1
} menu(){
cat <<END
.按单词出现的频率降序排序
.按字母出现的频率降序排序
END
read -p "Pls you choose num:" num
}
menu usage(){
echo "USAGE:You muset choose 1 or 2"
exit
} case "$num" in
)
word
;;
)
string
;;
*)
usage
esac
执行上述脚本,如果如下:
[root@mysql01 shell]# sh no_20.sh
1.按单词出现的频率降序排序
2.按字母出现的频率降序排序
Pls you choose num:1
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@mysql01 shell]# sh no_20.sh
1.按单词出现的频率降序排序
2.按字母出现的频率降序排序
Pls you choose num:2
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
用shell处理以下内容 1、按单词出现频率降序排序! 2、按字母出现频率降序排序! the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support的更多相关文章
- shell脚本,按字母出现频率降序排序。
[root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...
- shell读取文件内容并进行变量赋值
需求: shell读取文件内容,然后把内容赋值给变量然后进行字符串处理 实现: dataline=$(cat /root/data/data.txt) echo $dataline
- python3 获得shell的输出内容(subprocess.getstatusoutput)
默认通过os.system(“shell")命令赋值,结果是0之类的,0表示shell命令运行正确 如果想获得shell输出的内容,可以通过[subprocess.getstatusoutp ...
- Java实验--统计字母出现频率及其单词个数
本周的实验要求在之前实现统计单词的基础之上(可以见之前博客的统计单词的那个实验),对其进行修改成所需要的格式,统计字母出现频率的功能,并按照一定的格式把最终结果的用特定的格式在文本中显示出来 统计过程 ...
- [ SHELL编程 ] 文件内容大小写替换
shell编程经常会碰到字符串.文件内容大小写的转换,在不同的场景下选择合适的命令可以提高编程效率. 适用场景 需大小写转换的文件内容或字符串 字符串大小写替换 小写替换大写 echo "h ...
- shell读取文件内容
Shell脚本,执行解释速度快.代码简单易于理解.在shell代码编写过程中,经常会用到读取文件内容. 写法一: ------------------------------------ ...
- Shell 读取文本内容
在Linux中有很多方法逐行读取一个文件的方法,其中最常用的就是下面的脚本里的方法,而且是效率最高,使用最多的方法.为了给大家一个直观的感受,我们将通过生成一个大的文件的方式来检验各种方法的执行效率. ...
- Shell读取文件内容【转】
while read wOne wTwo wThreedo [ -z $wOne ] && continue #测试此行内容是否为空 xxx=$wOne ...
- Shell实现文件内容批量替换的方法
在Linux系统中,文件内容的批量替换同Windows平台相比要麻烦一点.不过这里可以通过Shell命令或脚本的方式实现批量替换的功能. 笔者使用过两个命令:perl和sed ,接下来会对其做出说明. ...
随机推荐
- Swift开发UITableView常用的一些细节知识点介绍
<code class="objectivec"><strong><span style="font-size:18px;"> ...
- H5学习之旅-H5的格式化(4)
H5的格式设置: b代表是粗体 i斜体 big 字体变大 small变小 em强调 strong 加强和变粗差不多 sub 定义下标字 sup 定义上标字 ins 插入字 del 删除字 代码实例 & ...
- 【一天一道LeetCode】#45. Jump Game II
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- IE11 开启F12开发人员工具中的 始终从服务器刷新
在进行页面脚本调试时,经常需要清除浏览器缓存来获取最新的脚本文件,IE11之前的浏览器版本我们可以按F12调出开发人员工具,在缓存选项中开启"始终从服务器中刷新"的设置后我们就不需 ...
- Java之美[从菜鸟到高手演变]之设计模式四
在阅读过程中有任何问题,请及时联系:egg. 邮箱:xtfggef@gmail.com 微博:http://weibo.com/xtfggef 转载请说明出处:http://blog.csdn.net ...
- 一起来搭简单的App框架
1.概述 最近在学车,抽着空写了一个常用的App框架,可惜不会制作gif图片,请忽略录屏软件的那个浮动窗口,要是有更好的方式制作gif麻烦告知下提前感谢,效果图如下,凑合着看下吧. 主要实现了 [1] ...
- android 之ViewStub
在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局.那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在 ...
- MTK GPIO 一些理解
基于6575平台: GPIO驱动程序提供了两个接口: (1)内核空间:所提供的GPIO驱动程序,驱动程序可以调用其他函数 (2)用户空间:用户空间的程序可以通过发送IOCTL /dev/ mtgp ...
- "《算法导论》之‘线性表’":双向循环链表
本文双链表介绍部分参考自博文数组.单链表和双链表介绍 以及 双向链表的C/C++/Java实现. 1 双链表介绍 双向链表(双链表)是链表的一种.和单链表一样,双链表也是由节点组成,它的每个数据结点中 ...
- android bitmap的内存分配和优化
首先Bitmap在Android虚拟机中的内存分配,在Google的网站上给出了下面的一段话 大致的意思也就是说,在Android3.0之前,Bitmap的内存分配分为两部分,一部分是分配在Dalvi ...