Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to create a script that replaces all letters of a word after certain word to 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.

share|improve this question

migrated from serverfault.com 2 hours ago

This question came from our site for system and network administrators.

If you insist to do it with sed:

echo 'Word: cat' | sed 'h; s/Word: .*/Word: /; x; s/.*Word: //; s/./*/g; H; x; s/\n//'

Result:

Word: ***
share|improve this answer

Using awk, you don't mention, specific use of sed :)

echo "Word: cat" | awk '{printf $1" "; for (i=1;i<=length($2);i++) printf "*"}'

Result:

Word: ***

share|improve this answer

You could to this using sed as follows:

echo 'Word: cat' |
sed -e '
   s/Word:/&\n/
   :loop
      s/\n\([^a-zA-Z]*\)[a-zA-Z]/\1*\n/
   tloop
   s/\n\([^a-zA-Z]*\)$/\1/
' 
share|improve this answer

You could use this time employing the hold space for manipulations, as:

sed -e '
   s/Word:/&\n/   # everything to the right of marker is asterisk zone
   h              # will need later on for reconstructing, so save orig
   s/.*\n//       # retain only the asterisk zone
   s/[a-zA-Z]/*/g # perform the asterisk operation
   H;g            # abut onto original data
   s/\n.*\n//     # peel off unwanted data
'
share|improve this answer

How about:

$ cat file
Word: cat
Word: foobar
$ sed -E 's/Word: *//; s/./*/g; s/^/Word: /' file
Word: ***
Word: ******

To modify the file in place, just use -i:

$ sed -E -i 's/Word: *//; s/./*/g; s/^/Word: /' file
share|improve this answer

An awk solution and a shell solution:

echo '"Hello: World"' | awk '{ gsub("[^\"]", "*", $2); print }'

We have to escape the " in [^\"] since the regular expression itself is in double quotes. This generates

"Hello: *****"

With the shell (at least bash and ksh93):

echo '"Hello: World"' | {
  read -r prefix rest
  printf '%s %s\n' "$prefix" "${rest//[^\"]/*}"
}

Assuming the text "Hello: World" is on a line of its own, but embedded in a larger text in the file greetings.txt:

awk '/"Hello: [^"]*"/ { gsub("[^\"]", "*", $2) } { print }' greetings.txt >greet.tmp && mv greet.tmp greeting.txt

or,

while read -r prefix rest; do
    if [[ "$prefix" =~ ^\"Hello: ]]; then
        rest=${rest//[^\"]/*}
    fi
    printf '%s %s\n' "$prefix" "$rest"
done <greetings.txt >greet.tmp
mv greet.tmp greetings.txt

For the input

Specific recipients:
"Hello: Mom!"
"Hello: Cat!"

General recipients:
"Hello: World!"

these two solutions generate

Specific recipients:
"Hello: ****"
"Hello: ****"

General recipients:
"Hello: ******"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.