|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『楼 主』:
怎么利用批处理替换文本文件中的指定内容.
使用 LLM 解释/回答一下
就是把A.TXT中的第一行字符,替换B.TXT中的###字符.
再把A.TXT中的第二行字符,替换B.TXT中的@@@字符.
一共有好几行需要替换
........................
这个能行的通吗,求高手指点.
多谢!
Last edited by pengfei on 2006-7-22 at 23:19 ]
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 ]
|
|
2006-7-22 17:54 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
───────────────── 版务记录 ─────────────────
执行:Will Sort
操作:移动主题:自 DOS疑難解答 & 問題討論(解答室)
说明:依照主题内容分类,更适合于发表在此版区
处罚:因属论坛新人,不予积分处罚,请访问 {7326}论坛新手必读,所有人的基本行为准则
───────────────── 版务记录 ─────────────────
───────────────── 版主提示 ─────────────────
为了便于论坛用户浏览和版主管理,请您修改本主题的标题,以简要叙述主题的内容或意图
如果您确认标题不需要修改,请继续回复该主题进行申辩,若申辩理由充分,将取消该通知
如果您确认标题需要修改,请点击该主题首帖下方"编辑"按钮,在编辑页面中修改标题栏
如果此通知发出三日后您仍未进行申诉或修改,将由版主进行修改,并对作者进行相应处罚
处罚为扣除8点积分,包括追回您因发表主题奖励的6分和因标题违规而惩罚性扣除的2分
在得知您做出积极恰当的回应后,版主将在三个工作日内删除本提示以消除对您的不利影响
───────────────── 版主提示 ─────────────────
───────────────── 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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-7-22 19:12 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
参考了5楼doscc的写法,修改了以前的代码,请注意使用条件:
@echo off
:: 如果A.txt第M行中含有在第N行中要替换的内容S(M<N),则会用S来替换B.txt中含有第M行中的匹配内容
:: 例如:假设A.txt中第1行含有在第二行中要替换的@@@,则会用@@@替换B.txt中含有第一行的@@@。
:: 使用格式:要用第M行的内容来替换S,就在第M行的位置上加一句 call :_replace S
:: 如果在第N行上没有要替换的内容,第N行的位置加 set num=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 ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-7-22 20:51 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
呵呵~ 我试一下,辛苦了.
Hehe~ I'll give it a try, thanks for your hard work.
|
|
2006-7-22 23:13 |
|
|
doscc
中级用户
  
积分 256
发帖 93
注册 2006-3-26 来自 广东
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
我也来一个
代码以 @ # $ 三个字符为例!
如果处理的文件更复杂. 最好用第三方工具!
@echo off & setlocal enabledelayedexpansion
copy a.txt a-BF.txt >NUL
copy b.txt b-BF.txt >NUL
call :nf @@@
call :nf ###
call :nf $$$
::还有要替换的字符写在这里以 "call :nf 字符" 的型式!注意有特殊意议的字符 如: | & 等...
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 ]
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 ]
|
|
2006-7-23 04:04 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Re doscc:
你的代码确实高,用到了call :标号 参数 的格式,这点应该向你学习。看了你的代码,有些地方还不是很懂,慢慢揣摩中。呵呵,水平有限就是这样,继续揣摩中:)
我的代码除了冒号,似乎不再惧怕任何敏感字符,只是如果要用N行内容来替换的话,势必要重复对第一行代码进行操作的代码格式,用call来解决应该是个不错的思路。哪位再来扩充一下吧。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-7-23 08:54 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Re namejm & doscc:
doscc 兄实现的是逐行替换,namejm 兄实现的是指定行号行替换,相对来言,前者的灵活性好,后者的效率更高。
对 namejm 兄的冒号问题,将 tokens=1,2改为tokens=1,2* 应该可以解决。不过这并不意味着可以解决所有的特殊字符问题,你最好找一个具有足够规模和复杂度的批处理代码(也可以是你的批处理自身)作为测试文本。
另外,建议将行号也作为 :_replace 的参数进行控制,以具有更大的扩展性。
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-7-23 13:12 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
厉害,试了一下,能行 咱们DOS联盟真是人才济济呀.
可是我刚入门,看得我是一头雾水,求 namejm 和 doscc 兄能解释一下这些命令的具体含义和用法,我在这里先谢谢了.
Last edited by pengfei on 2006-7-23 at 13:43 ]
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 ]
|
|
2006-7-23 13:37 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Re willsort:
冒号的问题,用tokens=1,2*确实好用,看来for语句还得好生揣摩;将行号也作为_replace的参数来控制的建议是个好思路,只可惜自己对call :标号 参数 的格式还一知半解,还是现学5楼doscc的,版主有好的办法,不妨贴出来让我们学习学习。
我所说的似乎不惧怕任何敏感字符,是指似乎无论A.txt或B.txt中是否含有什么敏感字符,都能处理,而不是说把@@@、###等要替换的内容换成敏感字符。可能是我描述得不够准确而让你误解了吧,也可能是我测试的文本不够复杂没有代表性。
我测试的是以下的文本
A.txt
第一行~`!@#$%^&*()-=_+/?>.<,|\
第二行
第三行&
第四行:
第五行
B.txt
::kkkldnldn##########khldn##k a mnaouoane@@@@@@&&&&44$4$$$$$$$$$><!@#$%^&*()\';,.?/`~|||\<<<>>
sldkla'$$ksoiakpa@@@@@##
sdkoanlkalm@224Y%**8####3&&&&@@@@@
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-7-23 14:11 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Re namejm:
指定行号的参数不难设置,将set /a num+=1 改为 set num=%2,然后将行号跟在 call :_replace @@@ 之后即可。要编写健壮的代码,还需要对引入的参数进行防空和类型判断,当然对于只是内部调用的模块代码,这个要求可以适当降低。
特殊字符的抵抗问题,建议你按照我上面所说的,将你的这段批处理本身保存为A.TXt/B.TXT 进行测试,相信你会有很多发现。
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-7-23 14:48 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
写这个代码的时候,反复的测试把我的脑袋都搞大了;拿自己写的代码保存为A.txt/B.txt来测试,妈呀,我都不认识谁是谁了。哎,碰到稍微复杂的问题,自己的脑袋就要短路,看来,当初不学理工科的选择也不是没根据呀。
哪位老大赶紧出马吧,我顶不住了。hoho。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-7-23 15:07 |
|
|
doscc
中级用户
  
积分 256
发帖 93
注册 2006-3-26 来自 广东
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
RE: namejm
兄改进后的代码. 很不错。向兄学习! 向版主学习!
RE: pengfei
代码不难. 注析较烦. 看置顶的 批处理教学.你很快就会理解的!
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!
|
|
2006-7-23 16:13 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
两位兄台改写的代码完全达到我预想的要求,只是还看不懂
请问,我能不能直接在批处理中输入字符,然后按顺序替换文本中的指定字符串
过两天再来请教
多谢各位了.
Last edited by pengfei on 2006-7-23 at 18:41 ]
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 ]
|
|
2006-7-23 18:40 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
请问,用SET /P直接指定一个变量%all%,在批处理中输入字符,然后用这个变量去替换文本文档中的指定字符,怎么写.
楼上两位大哥写的批处理,小弟还不怎么明白,正在学习中.
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.
|
|
2006-7-26 11:34 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
@echo off
setlocal enabledelayedexpansion
set file=
set /p file= 请输入要操作的文件名称(包括扩展名):
set "file=%file:"=%"
for %%i in ("%file%") do set file=%%~fi
echo.
set replaced=
set /p replaced= 请输入即将被替换的内容:
echo.
set all=
set /p all= 请输入替换字符串:
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%"
```
@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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-7-26 19:10 |
|