Bash的最后一个索引
对不起,我很抱歉,但是我似乎无法解决这个问题.
Sorry for the lame bash question, but I can't seem to be able to work it out.
我有以下简单情况:
-
我有像
artifact-1.2.3.zip
我想在连字符和点的最后一个索引(都是排他的)之间得到一个子字符串.
I would like to get a sub-string between the hyphen and the last index of the dot (both exclusive).
我的bash技能不太强.我有以下内容:
My bash skill are not too strong. I have the following:
a="artifact-1.2.3.zip"; b="-"; echo ${a:$(( $(expr index "$a" "$b" + 1) - $(expr length "$b") ))}
制作:
1.2.3.zip
如何也删除.zip
部分?
$ a="artifact-1.2.3.zip"; a="${a#*-}"; echo "${a%.*}"
只要
"#
模式"与$a
的开始相匹配,就会删除模式.
pattern 的语法类似于文件名匹配中使用的语法.
就我们而言,
‘#
pattern’ removes pattern so long as it matches the beginning of $a
.
The syntax of pattern is similar to that used in filename matching.
In our case,
-
*
是任意字符序列. -
-
表示文字破折号. - 因此,
#*-
匹配直到 first 破折号的所有内容. - 因此
${a#*-}
可以扩展为$a
可以扩展的范围, 除了从扩展中删除了artifact-
之外, 留下我们1.2.3.zip
.
-
*
is any sequence of characters. -
-
means a literal dash. - Thus
#*-
matches everything up to, and including, the first dash. - Thus
${a#*-}
expands to whatever$a
would expand to, except thatartifact-
is removed from the expansion, leaving us with1.2.3.zip
.
类似地,只要与扩展的 end 相匹配,"%
模式"就会删除模式.
就我们而言,
Similarly, ‘%
pattern’ removes pattern so long as it matches the end of the expansion.
In our case,
-
.
文字点. -
*
任何字符序列. - 因此
%.*
是包括字符串末尾的 last 点在内的所有内容. - 因此,如果
$a
扩展为1.2.3.zip
, 然后${a%.*}
扩展为1.2.3
.
-
.
a literal dot. -
*
any sequence of characters. - Thus
%.*
is everything including the last dot up to the end of the string. - Thus if
$a
expands to1.2.3.zip
, then${a%.*}
expands to1.2.3
.
工作完成.
此手册页的内容如下(至少在我的计算机上为YMMV):
The man page content for this is as follows (at least on my machine, YMMV):
${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.
HTH!
编辑
对@ x4d表示感谢,以获取详细的答案. 仍然认为人们应该使用RTFM. 如果他们不理解手册, 然后发布另一个问题.
Kudos to @x4d for the detailed answer. Still think people should RTFM though. If they don't understand the manual, then post another question.