简体   繁体   中英

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

By using sed how can I do this.Any one help me.

The following sed command will do what you want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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