Solved replacing a word with asterisk

Microsoft Windows xp professional w/serv...
December 10, 2009 at 21:37:42
Specs: Windows XP
Hi,
I have a big text file "input.txt" which contains a word in a line.eg.
ban
go
put
quiet
I want to generate a text file "output.txt" in the following manner:
"ban" => "***"
"go" => "**"
"put" => "***"
"quiet" => "*****"
so on..
I have the following code
for /f "tokens=*" %%w in ('type input.txt') do (echo "%%w" =^> "*********" >> output.txt)

but this does not replace word with exact number of "*".
plz help yhanks


See More: replacing a word with asterisk

Report •

#1
December 11, 2009 at 00:46:53
I have a big text file "input.txt" which contains a word in a line.eg.
ban
go
put
quiet

Do you mean that each line consists of just one word or that the words can be found on any line of text?


Report •

#2
December 11, 2009 at 08:23:28
yes, "input.txt" each line consists of just one word and the same is the case with the "output.txt" file.
thanks in advance.

Report •

#3
December 11, 2009 at 10:08:24
✔ Best Answer
Try this, not fully tested:

@echo off > output.txt
setlocal enabledelayedexpansion
cls

for /f "delims=*" %%1 in (input.txt) do (
    set var=%%1& call :length
)

:: Display output and Exit..

type output.txt
exit /b

:: Get length of input word and set number of *..

:length
set var1=%var%#
set pos=0
:start
        call set chr=%%var1:~%pos%,1%%
        if %chr%==# goto finis
        set /a pos+=1
        goto start

:finis
set stars=
for /l %%1 in (1,1,%pos%) do (
    set stars=!stars!*
)

:: Write to output file..

echo "%var%" =^> "!stars!">> output.txt

goto :eof

In my initial test the output file contained:

"ban" => "***"
"go" => "**"
"put" => "***"
"quiet" => "*****"


Report •

Related Solutions

#4
December 11, 2009 at 22:11:39
brilliant man brilliant!! Thanks a lot!

Report •

#5
December 11, 2009 at 22:43:57
You're very welcome - thanks for coming back with your comments.

Report •

#6
December 31, 2009 at 02:03:26
Hi Wahine!
Sorry for the problem you may face to follow up
this question as I asked it long time ago.

Recently,a small bug, I noticed while using your solution
which I will explain with an example.

Lets suppose a big list of word is as follows:

good
bad
ugly
smart
crying
happy
.
.
cricket
.
.
Now my problem; the program stops converting
after word cricket because its too sensitive regarding blank
space.

For eg. In above list word "cricket" is actually "cricket"
with single blank space, now the program considers this as
end of file and hence exits after word "cricket".
All the words before and including
cricket are converted.
I will be highly obliged If you make
necessary changes as I was not able to do so and my list is
very huge, so I am finding it very tiresome to look for words
with a blank space.
Thanks in advance.
Regards



Report •

#7
December 31, 2009 at 02:10:59
I mean word "cricket" is actually "cricket[b]" with a blank space
at the end.

Report •

#8
December 31, 2009 at 23:20:19
Yes, this is a result of how the command shell treats a space. You don't say whether or not you want the space to be shown in the output file e.g. "cricket" or "cricket[b]"

Report •

#9
January 1, 2010 at 01:24:32
Hi!
Thanks for quick reply, no sir I don't want it to be shown
on the output, so cricket [b] becomes "cricket" => "*******" .It
will be very helpful for me in future if you could make the code
to handle two words eg.
smart footballer becomes
"smart footballer" => "****************"
please note: [b] single blank also becomes *
Thanks and wish you a very happy
and prosperous New Year.

Report •

#10
January 1, 2010 at 13:22:57
Thank you for your friendly greetings which are fully reciprocated.

Try this, again not fully tested. I have used the '@' character as part of the coding, if this char appears in any input word the output of that word will be incorrect. If this occurs you should substitute a non-occurring char for @ (perhaps one of the Extended ASCII Codes). Good luck:

@echo off > output.txt
setlocal enabledelayedexpansion
cls

for /f "delims=*" %%1 in (input.txt) do (
    set var=%%1& call :length
)

:: Display output and Exit..
type output.txt
exit /b


:length
:: Remove trailing space(s) if exists..
:loop
if "!var:~-1!" equ " " set var=!var:~0,-1!&&goto loop

:: Set inter-word space(s) to @..
set var=%var: =@%

:: Get length of input word and set number of *..
set var1=%var%#
set pos=0
:start
        call set chr=%%var1:~%pos%,1%%
        if %chr%==# goto finis
        set /a pos+=1
        goto start

:finis
set stars=
for /l %%1 in (1,1,%pos%) do (
    set stars=!stars!*
)

:: Convert @ to inter-word space(s) and write to output file..
echo "%var:@= %" =^> "!stars!">> output.txt

goto :eof


Report •

Ask Question