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

shell学习笔记--字符串截取

2012-12-26 15:58 344 查看
其中有一些是我写的,其它都是汇总shell群里面的大牛写的,大家有更好的方法也可以提出来。

要求:把第一个数字和后面的“/”去掉

[root@yunwei14 scripts]# cat StringCut.txt
123/
456/
789/

脚本实例:

[root@yunwei14 scripts]# sed 's/^.//g;s/\///g' StringCut.txt
23
56
89
[root@yunwei14 scripts]# cat StringCut.txt |cut -c 2-3
23
56
89
[root@yunwei14 scripts]# cat StringCut.txt |while read String;do echo ${String:1:2};done
23
56
89
[root@yunwei14 scripts]# grep -oP '(?<=.).*(?=/)' StringCut.txt
23
56
89
[root@yunwei14 scripts]# for i in $(cat StringCut.txt);do expr substr "$i" 2 2; done
23
56
89
[root@yunwei14 scripts]# awk 'sub("^.","")sub("/",""){print $0}' StringCut.txt
23
56
89
[root@yunwei14 scripts]# awk 'BEGIN{FS=OFS=""}NF=3,sub(/^./,"")' StringCut.txt
23
56
89
[root@yunwei14 scripts]# awk '{print substr($0,2,2)}' StringCut.txt
23
56
89
[root@yunwei14 scripts]# awk -vFS='/' '$1=substr($1,2)' StringCut.txt
23
56
89
[root@yunwei14 scripts]# awk -F '/' '{print substr ($1,2,3)}' StringCut.txt
23
56
89


本文出自 “[reed@卢伟开~]#rm -rf /” 博客,请务必保留此出处http://luweikai.blog.51cto.com/1705672/1101000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: