Replace all letters in a word with ‘*’ after a certain word in a text file

sedtext processing

I need to create a script that replaces all letters of a word after a certain word with an asterisk (*). 
For example:

sed 's/Word:[^ ]\+/Word:*/' Desktop/script_test

But this script replaces the entire word with only one asterisk, while I want to replace all letters. How can I do that?
For example, with this input:

Word: cat

I want to get

Word: ***

I am running Linux.

P.S. The input must be read from a text file and also saved to the same file.

Best Answer

You could do it one at a time in a loop:

sed -e :1 -e 's/\(Word: *[^ ]*\)[^ *]/\1*/;t1'

To edit the file in place and assuming all the characters to replace are single-byte, you can make both sed's stdin and stdout the file as in:

sed ... < file 1<> file

Or with GNU sed, use the -i flag as in:

sed -i ... file

(Though that will replace the file with a new one (though with the same name). With BSD sed, use -i '' instead of -i).

Related Question