posts - 297,  comments - 15,  trackbacks - 0
得到长度

%x="abcd"
#方法一
%expr length $x
4
# 方法二
%echo ${#x}
4
# 方法三
%expr "$x" : ".*"
4
# expr 的帮助
# STRING : REGEXP   anchored pattern match of REGEXP in STRING

查找子串

%expr index  $x "b"
2
%expr index  $x "a"
1
%expr index  $x "b"
2
%expr index  $x "c"
3
%expr index  $x "d"
4

得到子字符串

# 方法一
# expr <string> startpos length
%expr substr "$x" 1 3
abc
%expr substr "$x" 1 5
abcd
%expr substr "$x" 2 5
bcd
# 方法二
# ${x:pos:lenght}
%echo ${x:1}
bcd
%echo ${x:2}
cd
%echo ${x:0}
abcd
%echo ${x:0:2}
ab
%pos=1
%len=2
%echo ${x:$pos:$len}
bc

匹配正则表达式

# 打印匹配长度
%expr match $x "."
1
%expr match $x "abc"
3
%expr match $x "bc"
0

字符串的掐头去尾

%x=aabbaarealwwvvww
%echo "${x%w*w}"
aabbaarealwwvv
%echo "${x%%w*w}"
aabbaareal
%echo "${x##a*a}"
lwwvvww
%echo "${x#a*a}"
bbaarealwwvvww

其中 , # 表示掐头, 因为键盘上 # 在 $ 的左面。
其中 , % 表示%,  因为键盘上 % 在 $ 的右面。
单个的表示最小匹配,双个表示最大匹配。
也就是说,当匹配的有多种方案的时候,选择匹配的最大长度还是最小长度。

字符串的替换

%x=abcdabcd
%echo ${x/a/b} # 只替换一个
bbcdabcd
%echo ${x//a/b} # 替换所有
bbcdbbcd

不可以使用 regexp , 只能用 * ? 的文件扩展方式。

转自:
http://bbs.chinaunix.net/viewthread.php?tid=201843
posted on 2009-10-05 18:07 chatler 阅读(170) 评论(0)  编辑 收藏 引用 所属分类: Shell

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年5月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(10)

随笔分类(307)

随笔档案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感觉这个博客还是不错,虽然做的东西和我不大相关,觉得看看还是有好处的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新评论

阅读排行榜

评论排行榜