简体   繁体   English

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

[英]How to use sed command to replace the string in linux?

I have String like below . 我有下面的字符串。

hd_ma_prod_customer_ro:*:123456789:john.doe

Basically I need to look for the last colon (:) and Replace everything after last colon(:) . 基本上,我需要寻找最后一个冒号(:)并替换最后一个冒号(:)之后的所有内容。

Output for the above string : hd_ma_prod_customer_ro:*:123456789:replacedString 以上字符串的输出: hd_ma_prod_customer_ro:*:123456789:replacedString

By using sed how can I do this.Any one help me. 通过使用sed我该怎么做。任何人都可以帮助我。

The following sed command will do what you want: 以下sed命令将执行您想要的操作:

s/:[^:]*$/:replacedString/

What it's replacing is a colon followed by any number of non-colons, at the end of the string. 它要替换的是在字符串末尾的冒号和任意数量的非冒号。

What it replaces it with is the colon plus your text. 用它代替的是冒号加您的文本。

See the following transcript for details: 有关详细信息,请参见以下成绩单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM