当前位置: 技术问答>linux和unix
请问SHELL中有能根据模式直接抽取指定字段的内置命令吗?不能使用awk等工具的!
来源: 互联网 发布时间:2015-03-31
本文导语: 比如,command "User 12.34 xxxxx" "User (*).*" 抽取的结果应为12;模式不一定是这么定义的 expr 好像有这样的功能,但不知具体怎么用的?? 谢谢帮忙啊! | 有啊,告诉你一定要给分啊,我可是...
比如,command "User 12.34 xxxxx" "User (*).*"
抽取的结果应为12;模式不一定是这么定义的
expr 好像有这样的功能,但不知具体怎么用的??
谢谢帮忙啊!
抽取的结果应为12;模式不一定是这么定义的
expr 好像有这样的功能,但不知具体怎么用的??
谢谢帮忙啊!
|
有啊,告诉你一定要给分啊,我可是新来的!嘿嘿
首先让我门看看你用的如果是bash的话,也就是你的系统是 redhat,恩,这样就方便的多了,你想想在这么多启动脚本里,抽取字段是十分必要的!我们看看/etc/init.d/functions里的一段把!
…………
[ -z "$gotbase" ] && base=${1##*/}
…………
显然是在/aa/bb/cc这样的地方抽取字段
然后我们转到看,man bash:
………………
${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 parameter 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 subscripted
with @ or *, the pattern removal operation is applied to each
member of the array in turn, and the expansion is the resultant
list.
${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in pathname
expansion. If the pattern matches a trailing portion of the
expanded value of parameter, then the result of the expansion is
the expanded value of parameter with the shortest matching pat-
tern (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 subscripted with @ or *, the pattern removal
operation is applied to each member of the array in turn, and
the expansion is the resultant list.
……………………………………………………………………………………
${parameter##word#}就有抽取字段功能
首先让我门看看你用的如果是bash的话,也就是你的系统是 redhat,恩,这样就方便的多了,你想想在这么多启动脚本里,抽取字段是十分必要的!我们看看/etc/init.d/functions里的一段把!
…………
[ -z "$gotbase" ] && base=${1##*/}
…………
显然是在/aa/bb/cc这样的地方抽取字段
然后我们转到看,man bash:
………………
${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 parameter 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 subscripted
with @ or *, the pattern removal operation is applied to each
member of the array in turn, and the expansion is the resultant
list.
${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in pathname
expansion. If the pattern matches a trailing portion of the
expanded value of parameter, then the result of the expansion is
the expanded value of parameter with the shortest matching pat-
tern (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 subscripted with @ or *, the pattern removal
operation is applied to each member of the array in turn, and
the expansion is the resultant list.
……………………………………………………………………………………
${parameter##word#}就有抽取字段功能
|
shell没有这种功能,要不然awk岂不是没有用处了?
还是老老实实用awk或者perl吧,一般的系统都配有这些包的
还是老老实实用awk或者perl吧,一般的系统都配有这些包的
|
楼主这个问题很奇怪,是否限定为shell的built_in功能,否则的话awk、sed还是expr有什么区别呢?
|
expr不是内置命令。