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-08-01 00:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use batch processing to replace specified content in a text file. View 14,001 Replies 23
Original Poster Posted 2006-07-22 17:54 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Just replace the characters in the first line of A.TXT with the ### characters in B.TXT.
Then replace the characters in the second line of A.TXT with the @@@ characters in B.TXT.
There are several lines that need to be replaced.
........................
Can this work? Please ask experts for guidance.
Thanks!

[ Last edited by pengfei on 2006-7-22 at 23:19 ]
Floor 2 Posted 2006-07-22 19:12 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

───────────────── Moderation Record ─────────────────
Executor: Will Sort
Operation: Move Topic: From DOS Troubleshooting & Problem Discussion (Help Desk)
Description: Classified according to the topic content, more suitable for posting in this forum area
Punishment: Since it is a new forum user, no points punishment, please visit {7326}Must-read for Forum Newcomers, Basic Code of Conduct for Everyone
───────────────── Moderation Record ─────────────────



───────────────── Moderator's Note ─────────────────
To facilitate forum users' browsing and moderator management, please modify the title of this topic to briefly describe the content or intention of the topic
If you confirm that the title does not need to be modified, please continue to reply to the topic for defense. If the defense reason is sufficient, this notice will be canceled
If you confirm that the title needs to be modified, please click the "Edit" button below the first post of the topic, and modify the title bar in the editing page
If you still do not appeal or modify within three days after this notice is issued, the moderator will modify it and impose corresponding punishment on the author
The punishment is to deduct 8 points, including 6 points recovered from the rewards for publishing the topic and 2 points deducted punitively for the title violation
After knowing that you have made a positive and appropriate response, the moderator will delete this notice within three working days to eliminate the adverse impact on you
───────────────── Moderator's Note ─────────────────
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2006-07-22 20:51 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Referencing the writing of doscc on floor 5, the previous code has been modified. Please note the usage conditions:

@echo off
:: If the content S to be replaced in line N is contained in line M of A.txt (M < N), then the content containing the matching content of line M in B.txt will be replaced with S
:: For example: Suppose line 1 of A.txt contains @@@ that needs to be replaced in line 2, then @@@ will be used to replace @@@ in line 1 of B.txt.
:: Usage format: To use the content of line M to replace S, add a line call :_replace S at the position of line M
:: If there is no content to be replaced on line N, add set num=N at the position of line N
setlocal enabledelayedexpansion
set num=0
call :_replace ###
call :_replace @@@
set num=3
call :_replace $$$
exit

:_replace
set /a num+=1
set char=%1
for /f "tokens=1,2* delims=:" %%i in ('findstr /n . A.txt') do if %%i equ %num% set str=%%j
for /f "delims=" %%i in (B.txt) do (
set _str=%%i
set "_str=!_str:%char%=%str%!"
echo !_str!>>tmp.txt
)
move tmp.txt B.txt


[ Last edited by namejm on 2006-7-23 at 16:29 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-07-22 23:13 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Hehe~ I'll give it a try, thanks for your hard work.
Floor 5 Posted 2006-07-23 04:04 ·  中国 北京 海淀区 IDC机房
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
I'll also give it a try.
Take the three characters @ # $ as an example!
If the file to be processed is more complex. It's best to use a third - party tool!

@echo off & setlocal enabledelayedexpansion
copy a.txt a - BF.txt > NUL
copy b.txt b - BF.txt > NUL
call :nf @@@
call :nf ###
call :nf $$$
::Write the characters that still need to be replaced here in the form of "call :nf character". Note that there are special - meaning characters such as: | &, etc...
goto :EOF

:nf
set str=%1
set / P line = < a.txt
more + 1 < a.txt >> _a.txt
move / Y _a.txt a.txt

for / f "delims=" %%i in (b.txt) do (
set _str = %%i
set _str =!_str:%str% = %line%!
echo!_str! >> 1.txt
)
move / Y 1.txt b.txt
goto :EOF


[ Last edited by doscc on 2006 - 7 - 23 at 04:38 ]
Floor 6 Posted 2006-07-23 08:54 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re doscc:

  Your code is really excellent, using the format of call :label parameter. I should learn from you in this regard. After looking at your code, there are some parts that I don't fully understand yet, and I'm gradually figuring them out. Hehe, it's just that my level is limited, so I'll keep figuring it out :)

  My code seems to be no longer afraid of any sensitive characters except for colons. However, if I need to replace N lines of content, I will inevitably have to repeat the code format for operating the first line of code. Using call to solve it should be a good idea. Someone else can expand on this.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-07-23 13:12 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm & doscc:

Brother doscc implemented line-by-line replacement, while Brother namejm implemented replacement of specified line numbers. Relatively speaking, the former has better flexibility, and the latter has higher efficiency.

For Brother namejm's colon problem, changing tokens=1,2 to tokens=1,2* should solve it. However, this doesn't mean all special character problems can be solved. You'd better find a batch processing code with sufficient scale and complexity (it can also be your own batch processing) as the test text.

In addition, it is suggested to also control the line number as a parameter of :_replace to have greater expandability.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2006-07-23 13:37 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Wow, it works after trying. Our China DOS Union is really full of talented people. But I'm just starting out and I'm completely confused. I hope namejm and doscc brothers can explain the specific meanings and usages of these commands. I'd like to thank you here in advance.

[ Last edited by pengfei on 2006-7-23 at 13:43 ]
Floor 9 Posted 2006-07-23 14:11 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re willsort:

  The problem with colons is that using tokens=1,2* works well. It seems that the for statement still needs to be carefully studied; the suggestion to use the line number as a parameter for _replace is a good idea. It's just that I still don't fully understand the format of call :label parameters. I'm still learning from the doscc in building 5. If the moderator has a good method, it's better to post it for us to learn.

  What I mean by seemingly not being afraid of any sensitive characters means that it seems it can handle whether A.txt or B.txt contains any sensitive characters, not that replacing the content to be replaced like @@@, ### with sensitive characters. Maybe my description was not accurate enough to cause your misunderstanding, or maybe the text I tested was not complex enough to be representative.

  The text I tested is the following
A.txt

First line~`!@#$%^&*()-=_+/?>.<,|\
Second line
Third line&
Fourth line:
Fifth line

B.txt

::kkkldnldn##########khldn##k a mnaouoane@@@@@@&&&&44$4$$$$$$$$$><!@#$%^&*()\';,.?/`~|||\<<<>>
sldkla'$$ksoiakpa@@@@@##
sdkoanlkalm@224Y%**8####3&&&&@@@@@
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-07-23 14:48 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

Setting parameters for a specified line number is not difficult. Change set /a num+=1 to set num=%2, and then follow the line number after call :_replace @@@. To write robust code, it is also necessary to perform null checks and type judgments on the introduced parameters. Of course, for module code that is only called internally, this requirement can be appropriately relaxed.

For the issue of resisting special characters, it is suggested that you follow what I said above, save your batch script itself as A.TXt/B.TXT for testing, and I believe you will have many discoveries.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 11 Posted 2006-07-23 15:07 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
When writing this code, repeatedly testing made my head big; I saved the code I wrote as A.txt/B.txt for testing, oh my god, I didn't even know which was which. Hey, when encountering slightly complicated problems, my mind just short-circuits. It seems that the choice of not studying science and engineering originally wasn't without reason.
  Any big shot hurry up and come to the rescue吧,I can't take it anymore. hoho.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-07-23 16:13 ·  中国 北京 海淀区 IDC机房
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
RE: namejm
The improved code by you is very good. Learning from you! Learning from the moderator!

RE: pengfei
The code is not difficult. The comments are tedious. Please refer to the top-sticked batch processing tutorial. You will understand it soon!
Floor 13 Posted 2006-07-23 18:40 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Two brothers' rewritten code completely meets my expected requirements, but I still can't understand it. May I ask, can I directly input characters in the batch processing and then replace the specified strings in the text in sequence? I'll ask for advice in a couple of days. Thanks, everyone.

[ Last edited by pengfei on 2006-7-23 at 18:41 ]
Floor 14 Posted 2006-07-26 11:34 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Excuse me, how to write a batch script that uses SET /P to directly specify a variable %all%, input characters in the batch, and then use this variable to replace the specified characters in a text document.

The batch scripts written by the two elder brothers above, the younger brother still doesn't quite understand, and is studying.
Floor 15 Posted 2006-07-26 19:10 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
```
@echo off
setlocal enabledelayedexpansion
set file=
set /p file= Please enter the name of the file to operate (including extension):
set "file=%file:"=%"
for %%i in ("%file%") do set file=%%~fi
echo.
set replaced=
set /p replaced= Please enter the content to be replaced:
echo.
set all=
set /p all= Please enter the replacement string:
for /f "delims=" %%i in ('type "%file%"') do (
set str=%%i
set "str=!str:%replaced%=%all%!"
echo !str!>>"%file%"_tmp.txt
)
copy "%file%" "%file%"_bak.txt >nul 2>nul
move "%file%"_tmp.txt "%file%"
start "" "%file%"
```
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: