shell编程系列2--字符串的处理

字符串的处理

.计算字符串的长度

方法1 ${#string}

方法2 expr length "$string"  (如果string中间有空格,必须加双引号)

例子:
# 通过${#string}获取字符串长度
[root@localhost shell]# var1="hello world"
[root@localhost shell]# len=${#var1}
[root@localhost shell]# echo $len [root@localhost shell]# len=`expr length "$var1"`
[root@localhost shell]# echo $len # expre length "$string"计算字符串长度
[root@localhost shell]# var2="hi shell"
[root@localhost shell]# len=`expr length "$var2"`
[root@localhost shell]# echo $len .获取子串在字符中的索引位置
语法:expr index $string $substring 例子:
[root@localhost shell]# var1="quickstart is a app"
[root@localhost shell]# inx=`expr index "$var1" start`
[root@localhost shell]# echo $inx # 从下面的例子可以看出来不是找子串的索引位置,而是获取字符的位置,即将 uniq 拆分成 u n i q 4个字符任意找到其中一个字符就返回这个字符的位置
[root@localhost shell]# inx=`expr index "$var1" uniq`
[root@localhost shell]# echo $inx .获取子串长度
expr match $string substr 例子:
# match语法从头开始匹配字符串,如果从中匹配到了就返回0
[root@localhost shell]# var1="quickstart is a app"
[root@localhost shell]# echo $var1
quickstart is a app
[root@localhost shell]# sub_len=`expr match "$var1" app`
[root@localhost shell]# echo $sub_len [root@localhost shell]# sub_len=`expr match "$var1" quick`
[root@localhost shell]# echo $sub_len [root@localhost shell]# sub_len=`expr match "$var1" quick.*`
[root@localhost shell]# echo $sub_len .抽取字符串中的子串
方法1
() ${string:position}
() ${string:position:length}
() ${string:-position} 或者 ${string:(position)} 方法2
expr substr $string $position $length 例子:
var1="kafka hadoop yarn mapreduce" # 提取var1中索引从10开始一直到结尾的字符串,索引下标从0开始
[root@localhost shell]# var1="kafka hadoop yarn mapreduce"
[root@localhost shell]#
[root@localhost shell]# echo $var1
kafka hadoop yarn mapreduce
[root@localhost shell]# sub_str1=${var1:}
[root@localhost shell]# echo $sub_str1
op yarn mapreduce # 从第10个位置开始提取5个字符串
[root@localhost shell]# sub_str2=${var1::}
[root@localhost shell]# echo $sub_str2
op ya # 取最后的5位,从-1开始
[root@localhost shell]# sub_str3=${var1: -}
[root@localhost shell]# echo $sub_str3
educe
[root@localhost shell]# sub_str3=${var1:(-)}
[root@localhost shell]# echo $sub_str3
educe # 取从最后开始取两位,注意 var1: - 之间有空格
[root@localhost shell]# sub_str3=${var1: -:}
[root@localhost shell]# echo $sub_str3
ed # 从10开始提取5位,索引从1开始
[root@localhost shell]# sub_str5=`expr substr "$var1" `
[root@localhost shell]# echo $sub_str5
oop y 注意:
使用expr,索引计数是从1开始计算
使用${string:position},索引计数是从0开始 练习: 需求描述:
变量 string="Bigdata process framework is Hadoop,Hadoop is an open source project"
执行脚本后,打印输出string字符串变量,并给出用户以下选项:
()、打印string长度
()、删除字符串中所有的Hadoop
()、替换第一个Hadoop为Mapreduce
()、替换全部Hadoop为Mapreduce 用户输入数字1|||,可以执行对应项中的功能;输入q|Q则退出交互模式 思路分析: 、将不同的功能模块划分,并编写函数、
function print_tips
function len_of_string
function del_hadoop
function rep_hadoop_mapreduce_first
function rep_hadoop_mapreduce_all 、实现第一步所定义的功能函数 #!/bin/bash
# string="Bigdata process framework is Hadoop,Hadoop is an open source project" function print_tips
{
echo "********************************************"
echo "(1)打印string长度"
echo "(2)删除字符串中所有的Hadoop"
echo "(3)替换第一个Hadoop为Mapreduce"
echo "(4)替换全部Hadoop为Mapreduce"
echo "********************************************"
} function len_of_string
{
echo "${#string}"
} function del_hadoop
{
# 把hadoop替换为空
echo "${string//Hadoop/}" } function rep_hadoop_mapreduce_first
{
echo "${string/Hadoop/Mapreduce}"
} function rep_hadoop_mapreduce_all
{
echo "${string//Hadoop/Mapreduce}"
} 、程序主流程的设计
[root@localhost shell]# cat example.sh
#!/bin/bash
# string="Bigdata process framework is Hadoop,Hadoop is an open source project" function print_tips
{
echo "********************************************"
echo "(1) 打印string长度"
echo "(2) 删除字符串中所有的Hadoop"
echo "(3) 替换第一个Hadoop为Mapreduce"
echo "(4) 替换全部Hadoop为Mapreduce"
echo "********************************************"
} function len_of_string
{
echo "${#string}"
} function del_hadoop
{
# 把hadoop替换为空
echo "${string//Hadoop/}" } function rep_hadoop_mapreduce_first
{
echo "${string/Hadoop/Mapreduce}"
} function rep_hadoop_mapreduce_all
{
echo "${string//Hadoop/Mapreduce}"
} while true
do
echo " 【string=$string】"
echo
print_tips
read -p "Pls input your choice(1|2|3|4|q|Q):" choice case $choice in
)
len_of_string
;;
)
del_hadoop
;;
)
rep_hadoop_mapreduce_first
;;
)
rep_hadoop_mapreduce_all
;;
q|Q)
exit
;;
*)
echo "Error,input only in {1|2|3|4|q|Q}"
;;
esac
done

shell编程系列2--字符串的处理的更多相关文章

  1. shell编程系列19--文本处理三剑客之awk中的字符串函数

    shell编程系列19--文本处理三剑客之awk中的字符串函数 字符串函数对照表(上) 函数名 解释 函数返回值 length(str) 计算字符串长度 整数长度值 index(str1,str2) ...

  2. shell编程系列4--有类型变量:字符串、只读类型、整数、数组

    shell编程系列4--有类型变量:字符串.只读类型.整数.数组 有类型变量总结: declare命令和typeset命令两者等价 declare.typeset命令都是用来定义变量类型的 decla ...

  3. (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单

    原文 (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单 接上一节:(C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开这 ...

  4. shell编程系列26--大型脚本工具开发实战

    shell编程系列26--大型脚本工具开发实战 大型脚本工具开发实战 拆分脚本功能,抽象函数 .function get_all_group 返回进程组列表字符串 .function get_all_ ...

  5. shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计

    shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计 shell中的数组的用法: shell数组中的下标是从0开始的 array=("Allen" & ...

  6. shell编程系列16--文本处理三剑客之awk模式匹配的两种方法

    shell编程系列16--文本处理三剑客之awk模式匹配的两种方法 awk的工作模式 第一种模式匹配:RegExp 第二种模式匹配:关系运算匹配 用法格式对照表 语法格式 含义 RegExp 按正则表 ...

  7. shell编程系列15--文本处理三剑客之awk格式化输出printf

    shell编程系列15--文本处理三剑客之awk格式化输出printf printf的格式说明符 格式符 含义 %s 打印字符串 %d 打印十进制数 %f 打印一个浮点数 %x 打印十六进制数 %o ...

  8. shell编程系列9--文本处理三剑客之sed概述及常见用法总结

    shell编程系列9--文本处理三剑客之sed概述及常见用法总结 sed的工作模式:对文本的行数据一行行处理,如下图 sed(stream editor),是流编辑器,依据特定的匹配模式,对文本逐行匹 ...

  9. shell编程系列6--shell中的函数

    shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...

随机推荐

  1. 深度学习环境搭建(CUDA9.0 + cudnn-9.0-linux-x64-v7 + tensorflow_gpu-1.8.0 + keras)

    关于计算机的硬件配置说明 推荐配置 如果您是高校学生或者高级研究人员,并且实验室或者个人资金充沛,建议您采用如下配置: 主板:X299型号或Z270型号 CPU: i7-6950X或i7-7700K ...

  2. 28.XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式

    转自https://www.cnblogs.com/gdjlc/archive/2013/09/08/3308229.html XML Schema 语言也称作 XML Schema 定义(XML S ...

  3. Caused by: java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [VCodeModel]

    2019-08-20 17:53:24,054 [ERROR] [http-nio-8047-exec-1] [HttpResult.java : 143] 系统异常 org.springframew ...

  4. JQuery实现品牌展示

    最近验收了ITOO,老师当时验收的时候对于界面的设计非常敏感,只要看了一个大体轮廓,就能给出我们建议,这是二十年积累的经验,我们要做的就是站在巨人的肩膀上,让我们成长更快! 老师说了一下关于界面设计的 ...

  5. LG4213 【模板】杜教筛(Sum)和 BZOJ4916 神犇和蒟蒻

    P4213 [模板]杜教筛(Sum) 题目描述 给定一个正整数$N(N\le2^{31}-1)$ 求 $$ans_1=\sum_{i=1}^n\varphi(i)$$ $$ans_2=\sum_{i= ...

  6. 关于跨域介绍和djiago解决跨域问题

    什么是跨域? 跨域,指的是浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制. 什么是同源策略? 同源策略又分为以下两种 DOM同源策略:禁止对不 ...

  7. 在运维中的shell经验总结

    来自良许Linux公众号 编写 脚本开头部分应有脚本功能说明.参数使用说明.作者姓名.创建/修改日期.版本信息,格式为: 脚本编写时,注意格式对齐,如所有的循环或者判断语句前后的语句进行对齐,以及ca ...

  8. [C++]线程池 与 [Go] mapreduce

    线程池 ref: https://github.com/progschj/ThreadPool/blob/master/ThreadPool.h ref: https://www.jianshu.co ...

  9. js 的数组怎么push一个对象. Js数组的操作push,pop,shift,unshift JavaScrip

    push()函数用于向当前数组的添加一个或多个元素,并返回新的数组长度.新的元素将会依次添加到数组的末尾. 该函数属于Array对象,所有主流浏览器均支持该函数. 语法 array.push( ite ...

  10. windows下的上帝模式,很好用,细想,很恐怖啊

    原文地址:https://blog.csdn.net/qq_43371556/article/details/101210501 上帝模式,即"God Mode”,或称为“完全控制面板”.是 ...