在linux中如何使用sed命令替换字符串?

在linux中如何使用sed命令替换字符串?,linux,Linux,我有下面这样的绳子 hd_ma_prod_customer_ro:*:123456789:john.doe 基本上,我需要查找最后一个冒号(:),并替换最后一个冒号(:)之后的所有内容 上述字符串的输出:hd\u ma\u prod\u customer\u ro::::123456789:replacedString 通过使用sed,我如何做到这一点。任何人都可以帮助我。以下sed命令将执行您想要的操作: s/:[^:]*$/:replacedString/ 它所替换的是一个冒号,后跟任意

我有下面这样的绳子

hd_ma_prod_customer_ro:*:123456789:john.doe
基本上,我需要查找最后一个冒号(:),并替换最后一个冒号(:)之后的所有内容

上述字符串的输出:
hd\u ma\u prod\u customer\u ro::::123456789:replacedString


通过使用sed,我如何做到这一点。任何人都可以帮助我。

以下
sed
命令将执行您想要的操作:

s/:[^:]*$/:replacedString/
它所替换的是一个冒号,后跟任意数量的非冒号,位于字符串的末尾

它所取代的是冒号加上文本

详情请参阅以下文字记录:

pax$ echo 'hd_ma_prod_customer_ro:*:123456789:john.doe' | sed 's/:[^:]*$/:replacedString/'
hd_ma_prod_customer_ro:*:123456789:replacedString

最好的方法是开始自己尝试,然后阅读您想要学习的命令的手册页:
mansed
。同时看一些例子也是一个很好的主意。