Skip to content
Advertisement

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.

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement