如何通过从整个字符串中减去一些字符来打印字符串中的某些字符。
ggc-clusterdb01.rvd.ukd.com ggc-clsdb02.rvd.ukd.com gg-akb.rvd.ukd.com
如何减去 '01.rvd.ukd.com'
或 '02.rvd.ukd.com'
,'03 这样的字符。 rvd.ukd.com'
..etc 从上面的主机名中打印剩余的名称,如 ggc-clsdb
或 ggc-clusterdb
。
我尝试了很多方法,但都没有成功。帮我举个例子。
要从字符串中删除后缀模式,请使用语法:${parameter%%word}
。
例子:
$ foo=ggc-clusterdb01.rvd.ukd.com
$ echo ${foo%%01.rvd.ukd.com}
ggc-clusterdb
您也可以使用通配符:
$ echo ${foo%%0*.rvd.ukd.com}
ggc-clusterdb
要删除点后的所有内容:
$ echo ${foo%%.*}
ggc-clusterdb
来自 man bash
的解释:
${parameter%%word}
Remove matching suffix pattern. 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 pattern (the ''%'' case) or
the longest matching pattern (the ''%%'' case) deleted.