shell编程系列2--字符串的处理
字符串的处理
1.计算字符串的长度
方法1 ${#string}
方法2 expr length "$string" (如果string中间有空格,必须加双引号)
例子:
# 通过${#string}获取字符串长度
[root@localhost shell]# var1="hello world"
[root@localhost shell]# len=${#var1}
[root@localhost shell]# echo $len
11
[root@localhost shell]# len=`expr length "$var1"`
[root@localhost shell]# echo $len
11
# expre length "$string"计算字符串长度
[root@localhost shell]# var2="hi shell"
[root@localhost shell]# len=`expr length "$var2"`
[root@localhost shell]# echo $len
8
2.获取子串在字符中的索引位置
语法: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
6
# 从下面的例子可以看出来不是找子串的索引位置,而是获取字符的位置,即将 uniq 拆分成 u n i q 4个字符任意找到其中一个字符就返回这个字符的位置
[root@localhost shell]# inx=`expr index "$var1" uniq`
[root@localhost shell]# echo $inx
1
3.获取子串长度
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
0
[root@localhost shell]# sub_len=`expr match "$var1" quick`
[root@localhost shell]# echo $sub_len
5
[root@localhost shell]# sub_len=`expr match "$var1" quick.*`
[root@localhost shell]# echo $sub_len
19
4.抽取字符串中的子串
方法1
(1) ${string:position}
(2) ${string:position:length}
(3) ${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:10}
[root@localhost shell]# echo $sub_str1
op yarn mapreduce
# 从第10个位置开始提取5个字符串
[root@localhost shell]# sub_str2=${var1:10:5}
[root@localhost shell]# echo $sub_str2
op ya
# 取最后的5位,从-1开始
[root@localhost shell]# sub_str3=${var1: -5}
[root@localhost shell]# echo $sub_str3
educe
[root@localhost shell]# sub_str3=${var1:(-5)}
[root@localhost shell]# echo $sub_str3
educe
# 取从最后开始取两位,注意 var1: -5 之间有空格
[root@localhost shell]# sub_str3=${var1: -5:2}
[root@localhost shell]# echo $sub_str3
ed
# 从10开始提取5位,索引从1开始
[root@localhost shell]# sub_str5=`expr substr "$var1" 10 5`
[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字符串变量,并给出用户以下选项:
(1)、打印string长度
(2)、删除字符串中所有的Hadoop
(3)、替换第一个Hadoop为Mapreduce
(4)、替换全部Hadoop为Mapreduce
用户输入数字1|2|3|4,可以执行对应项中的功能;输入q|Q则退出交互模式
思路分析:
1、将不同的功能模块划分,并编写函数、
function print_tips
function len_of_string
function del_hadoop
function rep_hadoop_mapreduce_first
function rep_hadoop_mapreduce_all
2、实现第一步所定义的功能函数
#!/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}"
}
3、程序主流程的设计
[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
1)
len_of_string
;;
2)
del_hadoop
;;
3)
rep_hadoop_mapreduce_first
;;
4)
rep_hadoop_mapreduce_all
;;
q|Q)
exit
;;
*)
echo "Error,input only in {1|2|3|4|q|Q}"
;;
esac
done