linux - 如何在 linux 中打印字符串中的特定字符?

标签 linux bash shell unix

<分区>

如何通过从整个字符串中减去一些字符来打印字符串中的某些字符。

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-clsdbggc-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.

关于linux - 如何在 linux 中打印字符串中的特定字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20854456/

相关文章:

shell - 如何在 Makefile 中定义全局 shell 函数?

mysql - Linux Tomcat MySQL 连接失败

regex - (Sed) 涉及负数的搜索/替换会产生额外字符

linux - 删除重复行但保留前 2 个实例

linux - 如何拖尾 w 命令

php - 脚本链接不传递参数

bash - SSH 文档 : bash prompt

linux - Centos shell 脚本 - 对以相同字符串开头的文件进行分组

linux - VirtualBox guest 无法解析主机名,但 git 可以

linux - 在linux中,如何为内存区域创建文件描述符