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. python中的嵌套类

    python中的嵌套类 在.NET和JAVA语言中看到过嵌套类的实现,作为外部类一个局部工具还是很有用的,今天在python也看到了很不错支持一下.动态语言中很好的嵌套类的实现,应该说嵌套类解决设计问 ...

  2. The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use

    java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more tha ...

  3. LG5325 【模板】Min_25筛

    P5325 [模板]Min_25筛 题目背景 模板题,无背景. 题目描述 定义积性函数$f(x)$,且$f(p^k)=p^k(p^k-1)$($p$是一个质数),求 $$\sum_{i=1}^n f( ...

  4. Selenium常用API的使用java语言之20-获取窗口截图

    自动化用例是由程序去执行,因此有时候打印的错误信息并不十分明确.如果在脚本执行出错的时候能对当前窗口截图保存,那么通过图片就可以非常直观地看出出错的原因. WebDriver提供了截图函数getScr ...

  5. Vue 项目目录结构分析

    Vue项目目录结构分析 ├── v-proj | ├── node_modules // 当前项目所有依赖,一般不可以移植给其他电脑环境 | ├── public | | ├── favicon.ic ...

  6. GBDT(梯度提升树) 原理小结

    在之前博客中,我们对Boosting家族的Adaboost算法做了总结,本文就对Boosting家族中另一个重要的算法梯度提升树(Gradient Boosting Decison Tree, 以下简 ...

  7. matlab的正则表达式

    第一部分——单个字符的匹配1 句点符号 '.' ——匹配任意一个(只有一个)字符(包括空格).例如:t.n,它匹配tan. ten.tin和ton,还匹配t#n.tpn甚至t nMatlab例子程序: ...

  8. 4、MapReduce思想、运行机制

    MapReduce 离线计算框架 分而治之 input > map > shuffle > reduce > output 分布式并行的计算框架 将计算过程分为两个阶段,Map ...

  9. 如何识别和解决SQL Server中的热闩锁(PAGELATCH_EX)

    描述 在SQL Server中,内部闩锁体系结构可在SQL操作期间保护内存.通过页面上的读写操作,可以确保内存结构的一致性.从根本上讲,它具有两个类:缓冲区锁存器和非缓冲区锁存器,它们在SQL Eng ...

  10. PHP流程控制之分支结构switch语句的使用

    分支结构switch语句的使用 还记得我们最开始讲了这么一个故事: 王同学家里头特别有钱,所以他的行程方式和正常人的又有些不一样. 他的出行方式呢有6种,如下: 1,司机开车2,民航3,自己家的专机4 ...