排序的字符串如下: 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的更多相关文章

  1. shell脚本,按字母出现频率降序排序。

    [root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...

  2. shell读取文件内容并进行变量赋值

    需求: shell读取文件内容,然后把内容赋值给变量然后进行字符串处理 实现: dataline=$(cat /root/data/data.txt) echo $dataline

  3. python3 获得shell的输出内容(subprocess.getstatusoutput)

    默认通过os.system(“shell")命令赋值,结果是0之类的,0表示shell命令运行正确 如果想获得shell输出的内容,可以通过[subprocess.getstatusoutp ...

  4. Java实验--统计字母出现频率及其单词个数

    本周的实验要求在之前实现统计单词的基础之上(可以见之前博客的统计单词的那个实验),对其进行修改成所需要的格式,统计字母出现频率的功能,并按照一定的格式把最终结果的用特定的格式在文本中显示出来 统计过程 ...

  5. [ SHELL编程 ] 文件内容大小写替换

    shell编程经常会碰到字符串.文件内容大小写的转换,在不同的场景下选择合适的命令可以提高编程效率. 适用场景 需大小写转换的文件内容或字符串 字符串大小写替换 小写替换大写 echo "h ...

  6. shell读取文件内容

           Shell脚本,执行解释速度快.代码简单易于理解.在shell代码编写过程中,经常会用到读取文件内容. 写法一: ------------------------------------ ...

  7. Shell 读取文本内容

    在Linux中有很多方法逐行读取一个文件的方法,其中最常用的就是下面的脚本里的方法,而且是效率最高,使用最多的方法.为了给大家一个直观的感受,我们将通过生成一个大的文件的方式来检验各种方法的执行效率. ...

  8. Shell读取文件内容【转】

    while read wOne wTwo wThreedo    [ -z $wOne ] && continue           #测试此行内容是否为空    xxx=$wOne ...

  9. Shell实现文件内容批量替换的方法

    在Linux系统中,文件内容的批量替换同Windows平台相比要麻烦一点.不过这里可以通过Shell命令或脚本的方式实现批量替换的功能. 笔者使用过两个命令:perl和sed ,接下来会对其做出说明. ...

随机推荐

  1. wordpress入门基础:wordpress文件系统结构详细介绍

    根目录|| wp-admin — wp-content — wp-includes|                    | |___________________________________ ...

  2. STL - vector容器

    1Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添加 ...

  3. LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal

    Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...

  4. Prefix tree

    Prefix tree The trie, or prefix tree, is a data structure for storing strings or other sequences in ...

  5. UML类图简介

    概述 设计模式中常常使用UML来表示类与类,类与接口之间的关系,UML类图是设计模式入门必备的技能,感觉各种关系比较多,这里做一下总结. 类与接口的表示 类与接口通常是一个矩形框表示,一般分为3层,第 ...

  6. javascript原始值和对象引用

    一句话来说:原始值是不可变的,而对象引用是可变的. js中的原始值(undefined.null.布尔值.数字和字符串)与对象(包括数组和函数)有着本质的区别.原始值是不可更改的,任何方法都无法更改一 ...

  7. React 深入系列5:事件处理

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列5:事件处理 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加 ...

  8. 使用javascript中读取Xml文件做成的一个二级联动菜单

    [html] view plaincopy <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...

  9. weblogic上JDBC的配置

    weblogic上JDBC的配置

  10. The 4 Essentials of Video Content Marketing Success

    https://www.entrepreneur.com/article/243208 As videos become increasingly popular, they provide the ...