免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 12607 | 回复: 13
打印 上一主题 下一主题

Linux shell脚本的字符串截取 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-07 17:57 |只看该作者 |倒序浏览
假设有变量 var=http://www.google.com/test.htm

一 # 号截取,删除左边字符,保留右边字符。http://blog.sina.com.cn/s/blog_4a071ed80100lvlu.html
echo ${var#*//}
其中 var 是变量名,# 号是运算符,*// 表示从左边开始删除第一个 // 号及左边的所有字符
即删除 http://
结果是 :www.google.com/test.htm

二 ## 号截取,删除左边字符,保留右边字符。
echo ${var##*/}
##*/ 表示从左边开始删除最后(最右边)一个 / 号及左边的所有字符
即删除 http://www.google.com/         
结果是 test.htm

三 %号截取,删除右边字符,保留左边字符
echo ${var%/*}
%/* 表示从右边开始,删除第一个 / 号及右边的字符
结果是:http://www.google.com

四 %% 号截取,删除右边字符,保留左边字符

echo ${var%%/*}
%%/* 表示从右边开始,删除最后(最左边)一个 / 号及右边的字符
结果是:http:

五 从左边第几个字符开始,及字符的个数
echo ${var:0:5}
其中的 0 表示左边第一个字符开始,5 表示字符的总个数。
结果是:http:

六 从左边第几个字符开始,一直到结束。
echo ${var:7}
其中的 7 表示左边第8个字符开始,一直到结束。
结果是 :www.google.com/test.htm

七 从右边第几个字符开始,及字符的个数
echo ${var:0-7:3}
其中的 0-7 表示右边算起第七个字符开始,3 表示字符的个数。
结果是:test

八 从右边第几个字符开始,一直到结束。
echo ${var:0-7}
表示从右边第七个字符开始,一直到结束。
结果是:test.htm

注:(左边的第一个字符是用 0 表示,右边的第一个字符用 0-1 表示)

论坛徽章:
0
2 [报告]
发表于 2010-12-08 16:31 |只看该作者
补充

echo ${#var}
计算字符串长度
结果是30

论坛徽章:
0
3 [报告]
发表于 2010-12-09 11:47 |只看该作者
原来如此,mark

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2010-12-11 11:50 |只看该作者
收藏此主题。

论坛徽章:
0
5 [报告]
发表于 2010-12-11 12:08 |只看该作者
先顶一下,然后再收藏

论坛徽章:
2
技术图书徽章
日期:2013-10-30 14:37:252015年亚洲杯之伊朗
日期:2015-04-30 13:37:02
6 [报告]
发表于 2010-12-11 15:02 |只看该作者
这么神奇的用法,是哪个SHELL的?

论坛徽章:
2
技术图书徽章
日期:2013-10-30 14:37:252015年亚洲杯之伊朗
日期:2015-04-30 13:37:02
7 [报告]
发表于 2010-12-11 15:03 |只看该作者
这么神奇的用法,是哪个SHELL的?

论坛徽章:
0
8 [报告]
发表于 2010-12-11 23:49 |只看该作者
Thanks for your collection. Come on!

论坛徽章:
0
9 [报告]
发表于 2010-12-11 23:52 |只看该作者
Thanks for your collection. Come on!

论坛徽章:
0
10 [报告]
发表于 2010-12-13 18:55 |只看该作者
原来manual里面都有的,只是人们懒得看,

${#parameter}
              The length in characters of the value of parameter is substituted.  If parameter is * or @, the value
              substituted  is  the number of positional parameters.  If parameter is an array name subscripted by *
              or @, the value substituted is the number of elements in the array.

       ${parameter#word}
       ${parameter##word}
              The word is expanded to produce a pattern just as in pathname expansion.  If the pattern matches  the
              beginning of the value of parameter, then the result of the expansion is the expanded value of param-
              eter with the shortest matching pattern (the ‘‘#’’ case) or the longest matching pattern (the  ‘‘##’’
              case)  deleted.   If parameter is @ or *, the pattern removal operation is applied to each positional
              parameter in turn, and the expansion is the resultant list.  If parameter is an array  variable  sub-
              scripted  with  @ or *, the pattern removal operation is applied to each member of the array in turn,
              and the expansion is the resultant list.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP