regex - 将字符串替换为星号 bash

标签 regex bash sed rhel

我正在尝试从用户那里获取路径作为输入。
用户将为特定应用输入特定路径:

script.sh /var/log/dbhome_1/md5

我想将目录数(在这种情况下为 1)转换为 *(星号)。稍后,脚本将在此路径上执行一些逻辑。

当我在输入上尝试 sed 时,我被数字卡住了 -

echo "/var/log/dbhome_1/md5" | sed "s/dbhome_*/dbhome_\*/g"

并且输入将是 -

/var/log/dbhome_*1/md5

我知道我在使用星号通配符和作为字符时遇到了一些问题... 也许正则表达式会在这里有所帮助?

最佳答案

GNU 代码:

sed "s#1/#\*/#"

.

$echo "/var/log/dbhome_1/md5" | sed "s#1/#\*/#"
"/var/log/dbhome_*/md5"

或者更一般的:

sed "s#[0-9]\+/#\*/#"

.

$echo "/var/log/dbhome_1234567890/md5" | sed "s#[0-9]\+/#\*/#"
"/var/log/dbhome_*/md5"

关于regex - 将字符串替换为星号 bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17394181/

相关文章:

regex - Linux 从命令行查找和替换

ruby - if 语句中 grep 返回值的赋值

javascript - 针对 RegExp 的 Google Closure 编译器警告

regex - 返回匹配 : regexp supported? 的逻辑向量

python - 为什么 bash 看不到我的文件?

bash - 如何在 unix 环境中填充超过 1024 个字符的文件?

bash - gnu 并行 : output each job to a different file

xml - 为特定标签替换 XML 文件中的值

perl - 如何让 iostat 表现得像 top

bash - 为什么使用 sed 命令不起作用?