操作字符串简析


操作字符串简析
 
Bash所支持的字符串操作的数量多的令人惊讶. 但是不幸的是, 这些工具缺乏统一的标准. 一些是参数替换的子集, 而另外一些则受到UNIX expr命令的影响. 这就导致了命令语法的不一致, 还会引起冗余的功能, 但是这些并没有引起混乱.
 
得到字符串长度的方法:
一${#string}
 
二expr length $string
三expr "$string" : '.*'
 
root@ubuntu:~/resource/shell-study/0507-2013# string="abcdefg"  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${#string}  
7  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr length $string`  
7  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr "$string" : '.*'`  
7  
root@ubuntu:~/resource/shell-study/0507-2013#  
 
学习一个方法:在一个文本文件的段落之间插入空行
 
#!/bin/bash  
  
MINLEN=3  
while read line  
do   
    echo "$line"  
    len=${#line}  
    if [ "$len" -lt "$MINLEN" ];then  
        echo   
    fi  
done  
  
exit 0  
结果:
root@ubuntu:~/resource/shell-study/0507-2013# ./test6.sh <file.txt   
12345  
12  
  
121321313  
12  
  
131234  
2  
  
123424155  
3rwq  
e  
  
wer  
weee  
ee  
  
e  
  
eeeerqwte  
root@ubuntu:~/resource/shell-study/0507-2013#   
匹配字符串开头的子串长度
方法一expr match "$string" '$substring'
$substring是一个正则表达式.
方法二expr "$string" : '$substring'
$substring是一个正则表达式.
 
 
索引expr index $string $substring通过这种方法在字符串$string中所匹配到的$substring第一次所出现的位置.
 
root@ubuntu:~/resource/shell-study/0507-2013# string=abcABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr index "$string" C12`  
6  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr index "$string" A`  
4  
root@ubuntu:~/resource/shell-study/0507-2013#  
 
提取子串
方法一
${string:position}
在$string中从位置$position开始提取子串,如果$string是"*"或者"@", 那么将会提取从位置$position开始的位置参数
${string:position:length}
在$string中从位置$position开始提取$length长度的子串.
root@ubuntu:~/resource/shell-study/0507-2013# string="abcABC123ABCabc"  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string:0}  
abcABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string:2}  
cABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string:8}  
3ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string:8:3}  
3AB  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string:-4}  
abcABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string:(-4)}  
Cabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string: -4}  
Cabc  
root@ubuntu:~/resource/shell-study/0507-2013#   
默认是提取整个字符串, 就象${parameter:-default}一样,所以倒数第三个命令返回整个字符串,但是使用圆括号或者添加一个空格可以"转义"这个位置参数,意识是打印倒数四个字符
如果$string参数是"*"或"@", 那么将会从$position位置开始提取$length个位置参数, 但是由于可能没有$length个位置参数了, 那么就有几个位置参数就提取几个位置参数
 
方法二:
 
expr substr $string $position $length
在$string中从$position开始提取$length长度的子串
root@ubuntu:~/resource/shell-study/0507-2013# string="abcABC123ABCabc"  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr substr $string 1 2`  
ab  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr substr $string 1 4`  
abcA  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr substr $string 4 3`  
ABC  
root@ubuntu:~/resource/shell-study/0507-2013#   
方法三:
expr match "$string" '\($substring\)'
从$string的开始位置提取$substring, $substring是正则表达式.
 
expr "$string" : '\($substring\)'
从$string的开始位置提取$substring, $substring是正则表达式.
root@ubuntu:~/resource/shell-study/0507-2013# string="abcABC123ABCabc"  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr match "$string" '\(.[b-c]*[A-Z]..[0-9]\)'`  
abcABC1  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr "$string" : '\(.[b-c]*[A-Z]..[0-9]\)'`  
abcABC1  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr "$string" : '\(.....\)'`  
abcAB  
root@ubuntu:~/resource/shell-study/0507-2013#   
方法四:
expr match "$string" '.*\($substring\)'
从$string的结尾提取$substring, $substring是正则表达式.
expr "$string" : '.*\($substring\)'
从$string的结尾提取$substring, $substring是正则表达式.
root@ubuntu:~/resource/shell-study/0507-2013# string="abcABC123ABCabc"  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr match "$string" '.*\([A-C][A-C][A-C][a-c]*\)'`  
ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr "$string" : '.*\([A-C][A-C][A-C][a-c]*\)'`  
ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo `expr "$string" : '.*\(.....\)'`  
BCabc  
root@ubuntu:~/resource/shell-study/0507-2013#   
子串削除
方法一:
${string#substring}
从$string的开头位置截掉最短匹配的$substring.
${string##substring}
从$string的开头位置截掉最长匹配的$substrin
root@ubuntu:~/resource/shell-study/0507-2013# string="abcABC123ABCabc"  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string#a*c}  
ABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string#a*C}  
123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string##a*C}  
abc  
root@ubuntu:~/resource/shell-study/0507-2013#   
方法二:
${string%substring}
从$string的结尾位置截掉最短匹配的$substring.
 
${string%%substring}
从$string的结尾位置截掉最长匹配的$substring.
root@ubuntu:~/resource/shell-study/0507-2013# string="abcABC123ABCabc"  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string%b*c}  
abcABC123ABCa  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string%%b*c}  
a  
root@ubuntu:~/resource/shell-study/0507-2013#   
子串替换
方法一:
${string/substring/replacement}
使用$replacement来替换第一个匹配的$substring.
 
${string//substring/replacement}
使用$replacement来替换所有匹配的$substring.
root@ubuntu:~/resource/shell-study/0507-2013# string="abcABC123ABCabc"  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string/abc/xyz}  
xyzABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string//abc/xyz}  
xyzABC123ABCxyz  
root@ubuntu:~/resource/shell-study/0507-2013#   
方法二:
${string/#substring/replacement}
如果$substring匹配$string的开头部分, 那么就用$replacement来替换$substring.
 
${string/%substring/replacement}
如果$substring匹配$string的结尾部分, 那么就用$replacement来替换$substring.
这种方法只处理开头部分或者结尾部分,对中间字符没有任何作用
root@ubuntu:~/resource/shell-study/0507-2013# string="abcABC123ABCabc"  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string/#abc/XYZ}  
XYZABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string/%abc/XYZ}  
abcABC123ABCXYZ  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string/%ABC/XYZ}  
abcABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013# echo ${string/#ABC/XYZ}  
abcABC123ABCabc  
root@ubuntu:~/resource/shell-study/0507-2013#   

相关内容

    暂无相关文章