标签:答案 ror hide onclick *** 对比 yarn 字符串 blog
例1:从头匹配最短删除
[root@localhost ~]# var1="I love you,Do you love me" [root@localhost ~]# echo ${var1#*ov} e you,Do you love me
例2:从头匹配最长删除
[root@localhost ~]# var1="I love you,Do you love me"
[root@localhost ~]# echo ${var1##*ov} e me
例3:后匹配最短删除
[root@localhost ~]# echo ${var1%ov*} I love you,Do you l
例4:从后匹配最长删除
[root@localhost ~]# echo ${var1%%ov*} I l
expr代表字符串,以下以string代表expr为例
注意使用expr索引下标从1开始
#当变量str未赋值时为string
[root@localhost ~]# echo ${str-string} string
#当变量str为空时为空 [root@localhost ~]# str= [root@localhost ~]# echo ${str-string}
#当变量str赋值后为str的值 [root@localhost ~]# str=123 [root@localhost ~]# echo ${str-string} 123
以下类推,生产环境中不常用
语法1:${#string}
语法2:expr length “$string”
[root@localhost ~]# var="Hello World" [root@localhost ~]# echo ${#var} 11 [root@localhost ~]# echo `expr length "$var"` 11
语法:expr index “$string” substr
默认时将start切割成s,t,a,r,t字符依次去在var中查找
所以uniq就被分割成u,n,i,q依次对比var变量中的第一个字符q,所以返回1
#查找start在var中的位置
[root@localhost ~]# var="quickstart is a app" [root@localhost ~]# echo `expr index "$var" start` 6
[root@localhost ~]# echo `expr index "$var" uniq`
1
语法:expr match “$string” substr
字串必须从头开始匹配
[root@localhost ~]# echo $var quickstart is a app [root@localhost ~]# echo `expr match "$var" app` 0 [root@localhost ~]# echo `expr match "$var" quicks` 6 [root@localhost ~]# echo `expr match "$var" quickstart` 10
例1:从第10位提取到最后,索引下标从0开始
效果与例4一样
[root@localhost ~]# var="kafka hadoop yarn mapreduce" [root@localhost ~]# echo ${var:10} op yarn mapreduce
例2:从第10位开始提取5位字符
[root@localhost ~]# echo ${var:10:5} op ya
例3:从右往左提取5个字符,下标从1开始,注意冒号后有空格
[root@localhost ~]# echo ${var: -5} educe
从右往左提取的5个字符中提取长度为2的字符串
[root@localhost ~]# echo ${var: -5:2} ed
例5:从左往右第10个字符开始提取长度为5的字符串
注意使用expr下标从1开始
[root@localhost ~]# echo `expr substr "$var" 10 5` oop y
小练习答案
[root@localhost ShellScript]# 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 "* (5) 重新打印 *" echo "***************************************" } function len_of_string { echo "${#string}" } function del_hadoop { echo "${string//Hadoop/}" } function rep_hadoop_mapreduce_first { echo "${string/Hadoop/Mapreduce}" } function rep_hadoop_mapreduce_all { echo "${string//Hadoop/Mapreduce}" } print_tips echo "【string=$string】" while true do read -p "Pls input your choice 1|2|3|4|5|q|Q :" choice case $choice in 1) len_of_string ;; 2) del_hadoop ;; 3) rep_hadoop_mapreduce_first ;; 4) rep_hadoop_mapreduce_all ;; 5) print_tips ;; q|Q) exit ;; *) echo "Error,input only in {1|2|3|4|q|Q}" ;; esac done
-d指定分隔符,默认使用tab键作为分隔符,-f取第几段
cat /etc/passwd | cut –d “:” –f 1
标签:答案 ror hide onclick *** 对比 yarn 字符串 blog
原文地址:https://www.cnblogs.com/tz90/p/12274816.html