shell 字符串操作的几个例子
2013-03-10 08:47
267 查看
shell 字符串操作的几个例子,包括取长度,读取内容,删除字符串,字符串的替换。
1.长度
[ttoy@tttt ~]$ test='I love china'
[ttoy@tttt ~]$ echo
${#test}
12
${#变量名}得到字符串长度
2.截取字串
[ttoy@localhost ~]$ test='I love china'
[ttoy@localhost ~]$ echo
${test:5}
e china
[ttoy@localhost ~]$ echo ${test:5:10}
e
china
${变量名:起始:长度}得到子字符串
3.字符串删除
[ttoy@localhost ~]$ test='c:/windows/boot.ini'
[ttoy@localhost ~]$
echo ${test#/}
c:/windows/boot.ini
[ttoy@localhost ~]$ echo
${test#*/}
windows/boot.ini
[ttoy@localhost ~]$ echo
${test##*/}
boot.ini
[ttoy@localhost ~]$ echo ${test%/*}
c:/windows
[ttoy@localhost
~]$ echo ${test%%/*}
${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。
${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。
注意:${test##*/},${test%/*} 分别是得到文件名,或者目录地址最简单方法。
4.字符串替换
[ttoy@localhost ~]$ test='c:/windows/boot.ini'
[ttoy@localhost ~]$
echo ${test/\//\\}
c:\windows/boot.ini
[ttoy@localhost ~]$ echo
${test//\//\\}
c:\windows\boot.ini
${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”\/”表示。
附:shell 字符串操作的高级变量
${#string}$string的长度
${string:position}在$string中, 从位置$position开始提取子串
${string:position:length}在$string中, 从位置$position开始提取长度为$length的子串
${string#substring}从变量$string的开头, 删除最短匹配$substring的子串
${string##substring}从变量$string的开头, 删除最长匹配$substring的子串
${string%substring}从变量$string的结尾, 删除最短匹配$substring的子串
${string%%substring}从变量$string的结尾, 删除最长匹配$substring的子串
${string/substring/replacement}使用$replacement, 来代替第一个匹配的$substring
${string//substring/replacement}使用$replacement, 代替所有匹配的$substring
${string/#substring/replacement}如果$string的前缀匹配$substring,
那么就用$replacement来代替匹配到的$substring ${string/%substring/replacement} 如果$string的后缀匹配$substring,那么就用$replacement来代替匹配到的$substring
说明:"*
$substring”可以是一个正则表达式.
1.长度
[ttoy@tttt ~]$ test='I love china'
[ttoy@tttt ~]$ echo
${#test}
12
${#变量名}得到字符串长度
2.截取字串
[ttoy@localhost ~]$ test='I love china'
[ttoy@localhost ~]$ echo
${test:5}
e china
[ttoy@localhost ~]$ echo ${test:5:10}
e
china
${变量名:起始:长度}得到子字符串
3.字符串删除
[ttoy@localhost ~]$ test='c:/windows/boot.ini'
[ttoy@localhost ~]$
echo ${test#/}
c:/windows/boot.ini
[ttoy@localhost ~]$ echo
${test#*/}
windows/boot.ini
[ttoy@localhost ~]$ echo
${test##*/}
boot.ini
[ttoy@localhost ~]$ echo ${test%/*}
c:/windows
[ttoy@localhost
~]$ echo ${test%%/*}
${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。
${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。
注意:${test##*/},${test%/*} 分别是得到文件名,或者目录地址最简单方法。
4.字符串替换
[ttoy@localhost ~]$ test='c:/windows/boot.ini'
[ttoy@localhost ~]$
echo ${test/\//\\}
c:\windows/boot.ini
[ttoy@localhost ~]$ echo
${test//\//\\}
c:\windows\boot.ini
${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”\/”表示。
附:shell 字符串操作的高级变量
${#string}$string的长度
${string:position}在$string中, 从位置$position开始提取子串
${string:position:length}在$string中, 从位置$position开始提取长度为$length的子串
${string#substring}从变量$string的开头, 删除最短匹配$substring的子串
${string##substring}从变量$string的开头, 删除最长匹配$substring的子串
${string%substring}从变量$string的结尾, 删除最短匹配$substring的子串
${string%%substring}从变量$string的结尾, 删除最长匹配$substring的子串
${string/substring/replacement}使用$replacement, 来代替第一个匹配的$substring
${string//substring/replacement}使用$replacement, 代替所有匹配的$substring
${string/#substring/replacement}如果$string的前缀匹配$substring,
那么就用$replacement来代替匹配到的$substring ${string/%substring/replacement} 如果$string的后缀匹配$substring,那么就用$replacement来代替匹配到的$substring
说明:"*
$substring”可以是一个正则表达式.
相关文章推荐
- 第3章 shell结构化命令------------(shell的数值比较、shell的字符串比较、shell的文件比较、shell的条件组合操作)
- shell 如何实现i++操作以及字符串截取操作
- shell 字符串操作
- 关于字符串操作的一个小例子(递归实现)
- Bash Shell字符串操作小结
- java操作json字符串 与 js操作json字符串简单例子
- Bash Shell字符串操作小结
- Shell基于模式匹配的字符串操作
- shell对参数操作 字符串操作
- javascript中常用的几个字符串操作详解
- shell下字符串的相关操作
- shell 字符串操作小结
- 两年前放在百度文库的几个初学shell脚本时写的几个简单例子
- shell脚本中字符串常用操作整理
- iOS开发中字符串处理成需要的格式的几个小例子
- 几个优化字符串操作性能的技巧
- shell 更改文件后缀-字符串操作
- shell 如何实现i++操作以及字符串截取操作
- 几个Shell脚本的例子
- shell变量与字符串操作