当前位置: > shell编程 >

shell 在指定位置给文本添加字符

时间:2014-11-27 15:25来源:linux.it.net.cn 作者:IT

问题:

给时间字符串 "20040816140105" 通过插入冒号变成:"20040816:14:01:05" 

解决方法:

1.使用sed:

echo 20040816140105 > tmp
cat tmp | sed -e 's/\(........\)\(..\)\(..\)\(..\)/\1:\2:\3:\4/'
20040816:14:01:05

2.使用echo:

str=20040816140105
new=$(echo "${str:0:4}:${str:4:2}:${str:6:2}:${str:8:2}:${str:10:2}:${str:12:2}")
echo $new
2004:08:16:14:01:05

 
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容