您的位置:首页 > 运维架构 > Shell

Shell(Bash) - 文件名.字符串截取

2009-11-23 23:37 435 查看
对于常用的字符串截取方式:
1.在LINUX中截取字符串方法

${string:position}
在string中从位置$position开始提取子串.
如果$string为"*"或"@",那么将提取从位置$position开始的位置参数

${string:position:length}
在string中从位置$position开始提取$length长度的子串.
例子:
read word
cut=${A:3:8}
echo "$word $cut"

2.在UNIX中截取字符串的方法
通过管道输出,通过cut处理字符串:
例子:
read word
cut=`echo $word|cut -c4-8`
echo "$word $cut"
或者使用awk截取字符串
例子:
read word
cut=`echo $word|awk '{print substr{$1,2,6}'`
echo "$word $cut"

3.调用expr命令,expr支持substr函数
expr substr "abcdef" 2 2

expr match "$string" '$substring'
$substring是一个正则表达式

expr "$string" : '$substring'
$substring是一个正则表达式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: