shell 字符串处理
2014-01-20 13:10
148 查看
1.获取字符串长度
${#string}
[jason@localhost ~]$ str="hello,world" [jason@localhost ~]$ echo ${#str} 11
2.获取子串
${string:position} ${string:position:length} $(string:(-postion)) 如果使用负数,表示从右开始计数,注意负数必须使用括号
[jason@localhost ~]$ str=ABCDEFGHIJKLMN [jason@localhost ~]$ echo ${str:1} BCDEFGHIJKLMN [jason@localhost ~]$ echo ${str:1:2} BC [jason@localhost ~]$ echo ${str:(-2)} MN
3.子串切除
${string#substring} 从左向右切除最短匹配的子串 ${stirng##substring} 从左向右切除最长匹配的子串 ${string%substring} 从右向左切除最短匹配的子串 ${stirng%%substring}
4.字符串正则提取
echo $string | grep -oE "regexpression”
[jason@localhost ~]$ str="I am 12 years old" [jason@localhost ~]$ echo $str | grep -Eo '[0-9]+' 12
5.字符串正则替换
echo $string | sed -r 's/regexpr/replacement'
[jason@localhost ~]$ str="I am 12 years old" [jason@localhost ~]$ echo $str | sed -r "s/ am/'m/" I'm 12 years old
6.分割字符串
awk
[jason@localhost ~]$ str="I_am_12_years_old, and you?" [jason@localhost ~]$ echo $str | awk -F '_' '{print $3}' 12 [jason@localhost ~]$ echo $str | awk '{split($1,a,"_");print a[3]}' 12
7. 获取索引
awk '{print match($0,"substring")}'
[jason@localhost ~]$ str="I_am_12_years_old, and you?" [jason@localhost ~]$ echo $str | awk '{print match($0,"[0-9]+")}' 6
注意这个索引是从1开始的
8.sed分组
sed的分组是很好玩的,在替换模式中,“&”代表前面匹配的全部字符串,而反斜杠加数字表示分组。[jason@localhost ~]$ str="name:jack;age:12" [jason@localhost ~]$ echo $str | sed -r 's/name:[^;]+/[&]/' [name:jack];age:12 [jason@localhost ~]$ echo $str | sed -r 's/name:([^;]+)/--\1--/' --jack--;age:12
9.awk常用字符串处理函数
sub(reg,replacement,string) gsub(reg,replacement,string)
将string中匹配正则表达式reg的字符串(全部)替换为replacement
[jason@localhost ~]$ str="name:jack;age:12" [jason@localhost ~]$ echo $str | awk '{gsub(/a/,"A",$0);print $0}' nAme:jAck;Age:12
index(substring,string) #返回子字符串substring在字符串string中的位置 length(string) #获取字符串的长度 match(string,reg) #获取匹配reg得到的子串在字符串中的位置 split(string,array,separator) #将字符串按分隔符separator分割到array数组中 sprintf("format",expression) #和c语言的sprintf类似 substr(string,position,length) #获取子字符串 tolower(string) toupper(string)
相关文章推荐
- shell中文件处理及字符串处理
- shell 自带的字符串处理功能
- (BA) SHELL 处理处理字符串的小结
- shell字符串处理
- shell(1):shell基础及函数,字符串,if,while,时间处理等应用
- Shell常用处理字符串方法(备查)
- Shell常用处理字符串方法
- Shell的字符串处理
- shell 字符串处理
- shell脚本处理字符串常用方法
- Shell中的正则表达式及字符串处理汇总:
- 【Shell脚本】字符串处理
- 第二章 Shell字符串处理之${}
- 转载:shell 字符串处理汇总(查找,替换等等)
- shell字符串处理
- shell字符串处理
- shell脚本编程 逐行处理文本 将字符串转化为数组
- Shell字符串处理规律总结
- shell中字符串的处理
- shell处理字符串的基本命令