China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-20 18:40
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Problems with text and letter substitution in files? View 5,835 Replies 29
Floor 16 Posted 2006-10-16 10:37 ·  中国 四川 成都 移动
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
It seems that the set command cannot distinguish between case sensitivity, and it's恐怕 difficult to achieve with pure batch processing. The thread starter might as well try the sed command provided by Brother Wunaike. There's no need to insist on solving it with pure batch processing, right?
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 17 Posted 2006-10-16 11:20 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Modified based on the code of moderator namejm on floor 9, which can distinguish case sensitivity.


@echo off
cd.>2.txt
setlocal enabledelayedexpansion
for /f "delims=" %%i in (test.txt) do (
set var=%%i
call :go
set "var=!var:1=o!"
set "var=!var:2=e!"
set "var=!var:3=u!"
set "var=!var:4=i!"
set "var=!var:5=u!"
set "var=!var:6=v!"
set "var=!var:7=l!"
set "var=!var:9=y!"
>>2.txt echo !var!
)
start 2.txt
exit

:go
set tmp=%var%@
set var=
:go_
if not "%tmp:~0,1%"=="@" (
set var_=%tmp:~0,1%
if "!var_!"=="A" set var_=啊
if "!var_!"=="a" set var_=阿
if "!var_!"=="B" set var_=不
if "!var_!"=="b" set var_=把
if "!var_!"=="C" set var_=才
if "!var_!"=="c" set var_=出
set var=!var!!var_!
set tmp=!tmp:~1!
goto go_
)
goto :eof


[ Last edited by pengfei on 2006-10-16 at 21:36 ]
Floor 18 Posted 2006-10-16 11:37 ·  中国 四川 成都 移动
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The code of pengfei is to replace by detecting each character, using the case-sensitive feature of if. The idea is really good.

It is still feasible to use this code to process small pieces of text content. If there is a lot of text content, the speed will be relatively slow. The poster can consider comprehensively.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 19 Posted 2006-10-16 11:41 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Originally posted by zouzhxi at 2006-10-15 22:51:
TO redtek:
This is a file someone gave me. They told me that I just need to replace the letters and numbers with specific characters to find out what it is. I have no way...

TO lxmxn:
You said use Notepad...


This file someone gave you, just use a tool to replace it~ : )
Because this is only a one-time thing. Your friend might only give you this type of file this once, not often.
And the decoding requires only a small part. Even if you do the automatic processing, it's just a half-finished product.
(From a psychological perspective: Making a half-finished product for a friend is unacceptable. It's not challenging or fulfilling, like doing menial work)
(       It's like walking halfway to a beautiful park and then going home halfway, haha)

It's better to use some tools to replace the one-time file~ : )

But I guess this might not be a one-time file processing.
It might be SQL storage private encoding and decoding to use,
or a simple encryption method to use on some JavaScript,
or a simple plaintext encryption code to use for chatting, maybe to prevent special character or command filtering or for easy processing,
or a custom encoding and decoding application like BASE64,
or a custom private encoding and decoding for a confidential document like a diary,
or a similar decoding required by the company for a certain file content (such as input method, code table... )

So, actually, the above problem. If the number of lines per line is not fixed, if there are tens of thousands of lines, and special symbols are included, etc.... Many problems will affect the solution speed and method of batch processing.
Example: The processing methods and algorithms for tens of thousands of lines and a few lines are different~ : )

So, if your content is not confidential, you can upload it all to let everyone help you think of a way together.
Please explain the very detailed application of this matter (if there are no private or enterprise secrets or other constraints)~ : )


) One-time conversion is still better with tools~ : )
) For daily use, it's convenient to use batch processing or call tools~ : )
) Must use DOS instead of other external tools. This is not very clear... (It seems you need to process these often, right?)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 20 Posted 2006-10-17 01:20 ·  IANA 局域网IP(Private-Use)
中级用户
★★
蝴蝶之吻
Credits 430
Posts 177
Joined 2006-09-20 12:00
19-year member
UID 63170
From 广东深圳
Status Offline
TO redtek:

Yes, the files my friend sent me are all like this. I have to extract them repeatedly, so I want to write a batch script... I don't want to use other tools (because it's cumbersome). I can execute the batch script at once...
Floor 21 Posted 2006-10-21 04:16 ·  IANA 局域网IP(Private-Use)
中级用户
★★
蝴蝶之吻
Credits 430
Posts 177
Joined 2006-09-20 12:00
19-year member
UID 63170
From 广东深圳
Status Offline
『Post 17』:

Modified based on the code of version master namejm on floor 9, which can distinguish case.
CODE:
--------------------------------------------------------------------------------

@echo off
cd.>2.txt
setlocal enabledelayedexpansion
for /f "delims=" %%i in (test.txt) do (
set var=%%i
call :go
set "var=!var:1=o!"
set "var=!var:2=e!"
set "var=!var:3=u!"
set "var=!var:4=i!"
set "var=!var:5=u!"
set "var=!var:6=v!"
set "var=!var:7=l!"
set "var=!var:9=y!"
>>2.txt echo !var!
)
start 2.txt
exit

:go
set tmp=%var%@
set var=
:go_
if not "%tmp:~0,1%"=="@" (
set var_=%tmp:~0,1%
if "!var_!"=="A" set var_=啊
if "!var_!"=="a" set var_=阿
if "!var_!"=="B" set var_=不
if "!var_!"=="b" set var_=把
if "!var_!"=="C" set var_=才
if "!var_!"=="c" set var_=出
set var=!var!!var_!
set tmp=!tmp:~1!
goto go_
)
goto :eof



I've been working on it for a long time, and the above method is too slow. Not ideal. Is there a more streamlined one, .
Floor 22 Posted 2006-10-21 22:38 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
There is another method (just the principle):

) Read all variables into an array (can simulate array functionality)
) Jump directly with the array content as the target label

) Uppercase A and lowercase a both point to label A. Inside label A, use IF to judge the case and replace.
Advantage: The two-choice method is only for specified letters, saving the time of judging each letter with IF.

That is to say: As long as the replacement content is divided into case, one judgment is enough:
Because: If the IF judges that the letter is uppercase (each array only holds one character, eliminating the extraction operation of :~),
then it will be replaced in uppercase, otherwise it must be lowercase! So one judgment solves it.

If other symbols are encountered, it doesn't matter! Anyway, it jumps to the label belonging to it according to the array content, and you can replace as you like inside the label.

) After the entire array is processed, it means that the variables in the array in memory have been completely changed and replaced.
) Then use a for loop to write the array content back to the file from the beginning to the end.
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 23 Posted 2006-10-21 22:46 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
For example:

) Extract all text into an array, with each character occupying one array element.

) Jump with the following tag:

CALL :The content of array element 1

Just this line is enough:)

Below are the tags:

:A
In tag A, perform an IF judgment to see if it is uppercase (if yes: replace what character),
If it is not uppercase, it must be lowercase, replace what.
(So a judgment is done here)

:1 In tag 1, no need for judgment, just replace directly.
Advantage: Universal, replace whatever you want.

) How many more contents to replace are added to the tag:)

) Finally, after the array loop has gone through, write the text with a FOR.
As for the problem of whether the content written into the text is one element per line (how many elements per line)
The same can be calculated and += what comes out.
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 24 Posted 2006-11-12 19:06 ·  中国 浙江 杭州 滨江区 电信
新手上路
Credits 2
Posts 1
Joined 2006-11-12 17:54
19-year member
UID 70344
Gender Male
Status Offline
Just use the replace function in Notepad.
Floor 25 Posted 2006-11-13 01:29 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Using sed to solve such problems is really the best!
Floor 26 Posted 2006-11-15 03:58 ·  IANA 局域网IP(Private-Use)
中级用户
★★
蝴蝶之吻
Credits 430
Posts 177
Joined 2006-09-20 12:00
19-year member
UID 63170
From 广东深圳
Status Offline
This problem was almost forgotten...
This problem is not solved...
I'm still depressed...
Is there a better way...
......
Floor 27 Posted 2006-11-16 03:58 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Why hasn't it been solved yet even though the moderator posted very detailed information?
Floor 28 Posted 2006-11-16 04:00 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
It can be written like this if you don't mind the trouble:

sed "s/A/啊/g;s/a/阿/g;......................" life (..........continue to add)
Floor 29 Posted 2006-11-23 06:30
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 30 Posted 2007-09-06 09:49 ·  中国 广东 汕头 电信
新手上路
Credits 4
Posts 2
Joined 2007-08-31 03:11
18-year member
UID 96233
Gender Male
Status Offline
Isn't there a find and replace function in the text document?
Forum Jump: