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. SpringCloud学习心得—1.2—Eureka注册中心的密码认证、高可用的设置

      SpringCloud学习心得—1.2—Eureka注册中心的密码认证.高可用的设置 这是相关代码 链接 Eureka开启密码配置 添加依赖 <dependency> <grou ...

  2. python3_pygame游戏窗口创建

    python3利用第三方模块pygame创建游戏窗口 步骤1.导入pygame模块 步骤2.初始化pygame模块 步骤3.设置游戏窗口大小 步骤4.定义游戏窗口背景颜色 步骤5.开始循环检测游戏窗口 ...

  3. java连接mysql数据库时的时区设置问题(time_zone)

    java在连接mysql数据库时,会由于时区设置不正确导致报以下的错误:   The server time zone value '???ú±ê×??±??' is unrecognized or ...

  4. 《The One!团队》第八次团队作业:Alpha冲刺

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握软件测试基础技术.(2)学习迭代式增量软件开发过程(Scrum) 团队项 ...

  5. vue 博客知识点汇总

    1. vue修改url,页面不刷新 项目中经常会用到同一个页面,结构是相同的,我只是在vue-router中通过添加参数的方式来区分状态,参数可以在页面跳转时带上params,或者query,但是有一 ...

  6. vbs查找Excel中的Sheet2工作表是否存在不存在新建

    set oExcel = CreateObject( "Excel.Application" ) oExcel.Visible = false '4) 打开已存在的工作簿: oEx ...

  7. 「NOI2012」骑行川藏

    「NOI2012」骑行川藏 题目描述 蛋蛋非常热衷于挑战自我,今年暑假他准备沿川藏线骑着自行车从成都前往拉萨. 川藏线的沿途有着非常美丽的风景,但在这一路上也有着很多的艰难险阻,路况变化多端,而蛋蛋的 ...

  8. java8中的流操作

    https://www.ibm.com/developerworks/cn/java/j-experience-stream/index.html Stream 流是 Java 8 新提供给开发者的一 ...

  9. nginx配置静态资源:配置绝对路径

    nginx配置静态资源:配置绝对路径 项目都是html格式的文件,我的项目路径:E:\javaservice\nginx-1.15.7\html assets:静态资源 html:站点文件 uploa ...

  10. [NgRx] NgRx Data Fetching Solution - How to Load Data Only If Needed

    We have a reoslver, which everytime we want visit '/courses' route, it will be triggered, then api w ...