字符串操作、数组、正则使用

1、字符串截取的三种用法
1)${var:起始位置:长度} //从第0位开始

[root@server0 ~]# X=135556684456
[root@server0 ~]# echo ${#X} #获取变量x有多少位
12
[root@server0 ~]# echo ${X:0:4}
1355
2expr substr "$var" 起始位置 长度 #该方法起始从1开始
[root@server0 ~]# expr substr $X 2 4
3555
3echo $var | cut -b 起始位置-结束位置 #该方法起始从1开始
[root@server0 ~]# echo $X | cut -b 4-6
556

2、字符串的替换
1)只替换第1个子串
格式:${var/old/new}

[root@server0 ~]# x=123456123456
[root@server0 ~]# echo ${x/3/*}
12*456123456

2)替换全部子串
格式:${var//old/new}
[root@server0 ~]# echo ${x//3/*}
12*45612*456
(替换不影响x的值,只会影响输出的显示效果)

3、字符串的匹配删除
1)从左向右,最短匹配删除(掐头)
格式:${变量名#*关键词}
删除从左侧第1个字符到最近的关键词的部分,* 作通配符理解:
[root@server0 ~]# x='root:x:0:0:root:/root:/bin/bash'
[root@server0 ~]# echo ${x#*:} //只删除最近:之前的
x:0:0:root:/root:/bin/bash
2)从左向右,最长匹配删除
[root@server0 ~]# echo ${x##*:} //删除到:之前所有的
/bin/bash
3)从右向左,最短匹配删除(去尾)
[root@server0 ~]# echo ${x%:*}
root:x:0:0:root:/root
4)从右向左,最长匹配删除
[root@server0 ~]# echo ${x%%:*}
root

4、字符串初值的处理
通过${var:-word}判断变量是否存在,决定是否给变量赋初始值
若变量var已存在且非Null,则返回 $var 的值;否则返回字串“word”,原变量var的值不受影响。
[root@server0 ~]# echo ${NB:-123} //var值不存在
123
[root@server0 ~]# NB=hehe //var值存在
[root@server0 ~]# echo ${NB:-123} //输出原变量的值
hehe

5、数组
整体赋值的格式为“数组名=(值1 值2 值3 .. ..)”
[root@server0 ~]# x=(11 22 33 44 55 66)
[root@server0 ~]# echo ${x[0]}
也可以直接为单个数组元素赋值,格式为“数组名[下标]=值”
[root@server0 ~]# x[0]=1
[root@server0 ~]# x[1]=2
[root@server0 ~]# x[2]=3
变量名里不能再包含变量(例如x$i)
查看数组内的内容:
[root@server0 ~]# echo ${x[*]}
查看数组的长度:
[root@server0 ~]# echo ${#x[*]}
[root@server0 ~]# echo ${#x[@]}

6、正则表达式
6.1 字符类
. 匹配任意单个字符,可以取出非空行
* :例如 grep “a*” a.txt //匹配a出现的(a,aa,aaa)
.* 代表匹配任意所有 空行,非空行一起取出
[]集合查找的是单个字符
[[:xxxxx:]] grep工具预定义的一些命名字符
[[:digit:]] 匹配一个数字
[[:alpha:]] 匹配一个字母
[root@localhost home]# echo "ss0627" | grep '[[:digit:]]'
ss0627
[root@localhost home]# echo "ss0627" | grep '[[:alpha:]]'
ss0627
6.2 数量限定类
? 紧跟在它前面的单元应匹配零次或一次
cat /etc/passwd | grep -E 'ros?' // 匹配出的可以不含s
+ 紧跟在它前面的单元应匹配一次或多次
cat /etc/passwd | grep -E 'ros+' //匹配出的可以包含s
*紧跟在它前面的单元应匹配零次或多次
{N} 紧跟在它前面的单元应精确匹配N次
cat /etc/passwd | grep -E 'ro{2}' // 匹配o出现2次
{N,} 紧跟在它前面的单元至少匹配N次
{,M}紧跟在它前面的单元最多匹配M次
{N,M}紧跟在它前面的单元匹配N到M次,最少N次,最多M次
6.3 位置限定符
^ 匹配行首的位置
$ 匹配行末的位置
\< 匹配单词开头的位置
free | grep -E '\<Mem'
\> 匹配单词结尾的位置
df -h | grep -E 'boot\>'
\b 匹配单词的开头或者结尾的位置
[root@localhost home]# echo -e 'athome\nhaveat\natdidiat' > test.txt
[root@localhost home]# cat test.txt |grep -E '\bat' \\输出at开头的单词
athome
atdidiat
[root@localhost home]# cat test.txt |grep -E 'at\b' \\输出以at结尾的单词
haveat
atdidiat
[root@localhost home]# cat test.txt |grep -E '\bat\b' \\无输出
\B 匹配非单词的开头或者结尾的位置
[root@localhost home]# cat test.txt | grep -E '\Bat' \\输出非at为开头的包含at的单词
haveat
atdidiat
[root@localhost home]# cat test.txt | grep -E 'at\B' \\输出非at为结尾的包含at的单词
athome
atdidiat

最开始和最后一个字母对调
[root@server0 ~]# sed -r 's/^(.)(.*)(.)$/\3\2\1/' 1.txt

6.4 特殊字符
\ 转义字符
( ) 将正则表达式的一部分括起来组成一个单元,可以对整个单元使用数量限定符
([0-9]{1,3}\.){3}[0-9]{1,3} 匹配ip地址
ifconfig | grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}'
| 连接两个子表达式,表示或的关系

7、扩展正则
(简化基本,扩展新的)
? 前面字符出现0或者1次
+ 前面字符出现1次或多次
() 整体 (abc)+ abc abcabc ...
\b 单词边界,精确过滤
[root@room8pc205 桌面]# grep 'the' 1.txt #无论the在哪都匹配
hello the world
theapple is great
tast atheorang
[root@room8pc205 桌面]# grep '\bthe' 1.txt #只匹配the前没有字符
hello the world
theapple is great
[root@room8pc205 桌面]# grep '\bthe\b' 1.txt
hello the world
扩展正则:简单,兼容性差(并不是)
基本正则:复杂,兼容性强(所有软件都支持)

猜你喜欢

转载自www.cnblogs.com/cp-linux/p/13200500.html