中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-25 23:05
47,813 topics / 349,918 posts / today 0 new / 48,275 members
DOS批处理 & 脚本技术(批处理室) » Replacement of conditional text files [Answered]
Printable Version  3,721 / 45
Floor1 123cainiao Posted 2007-09-23 23:21
初级用户 Posts 41 Credits 97
There are 2 TXT files: ab.txt and cd.txt.

The content of ab.txt is as follows:
Name "Zhang San", ID '102', tel '0102211', 0, 25, 11
Name "Li Sida", ID '102', tel '0202211', 0, 25, 11
Name "Wang Er", ID '102', tel '01023311', 0, 25, 11
........

The content of cd.txt is as follows:
Zhao Kuo
Wang Bin
Li Ling
.......

Can batch processing commands be used to extract the name from each line of cd.txt to replace the name content in each line of ab.txt. So that the finally generated ab.txt file is as follows:
Name "Zhao Kuo", ID '102', tel '0102211', 0, 25, 11
Name "Wang Bin", ID '102', tel '0202211', 0, 25, 11
Name "Li Ling", ID '102', tel '01023311', 0, 25, 11
........

[ Last edited by 123cainiao on 2007-9-25 at 01:36 PM ]
Floor2 yoyodos Posted 2007-09-24 11:03
初级用户 Posts 59 Credits 128
It feels a bit difficult, ha, I'll take a look again
Floor3 123cainiao Posted 2007-09-24 11:09
初级用户 Posts 41 Credits 97
On the forum, when searching for relevant content, an article that can be referred to was found; the excerpt is as follows:

Text editing (line-by-line replacement AB text integration)

File name: A.txt
Content is as follows:
old("A")new("A")
old("A")new("A")
old("A")new("A")
old("A")new("A")
old("A")new("A")
...

File name: B.txt
Content is as follows:
good
bad
poor
...

Need to replace A line by line to
old("good")new("good")
old("bad")new("bad")
old("poor")new("poor")
....

The referenced answer:
@echo off & setlocal enabledelayedexpansion
set data=old("A")new("A")
for /f "tokens=* " %%i in (b.txt) do (
set data=!data:A=%%i!
echo !data!>>c.txt
set data=!data:%%i=A!
)
pause

But the above obviously cannot handle the problem I raised, mainly because the replacement format is different, please everyone help!
Floor4 yoyodos Posted 2007-09-24 11:12
初级用户 Posts 59 Credits 128
Well, it's a bit different because your file requires each string to be replaced, not a unified replacement. It's a bit difficult, haha. I'm wondering if I can figure it out.
Floor5 wudixin96 Posted 2007-09-24 11:41
银牌会员 Posts 931 Credits 1,928
```
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%a in ('findstr /n .* ab.txt') do (
for /f "tokens=1* delims=:" %%i in ('findstr /n .* cd.txt^|find "%%a:"') do set str=%%j
set ln=%%b
call set string=%%ln:"=#%%
call set string=%%string:"=#%%
for /f "tokens=2 delims=#" %%i in ("!string!") do set str1=%%i
call :js
)
pause
goto :eof

:js
set ln=!ln:%str1%=%str%!
echo %ln%
```
Floor6 yoyodos Posted 2007-09-24 11:52
初级用户 Posts 59 Credits 128
Study! So powerful
Floor7 123cainiao Posted 2007-09-24 12:07
初级用户 Posts 41 Credits 97
Thank you very much, wudixin96. It's really eye-opening.
Floor8 yoyodos Posted 2007-09-24 12:11
初级用户 Posts 59 Credits 128
This is also something that must be implemented with call, otherwise there's no way to assign a value in the replacement variable, right? Asking wudixin96 a question: What does the call in front of call set string=%%ln:"=#%% mean? Thanks!
Floor9 lxmxn Posted 2007-09-24 12:12
版主 Posts 4,938 Credits 11,386
```gawk -F "BEGIN{OFS=\"\"}{if(FNR==NR){str=$0} else { a+=1; $2=\"\x22\" str \""\"; print $0}}" cd.txt ab.txt```
Floor10 yoyodos Posted 2007-09-24 12:14
初级用户 Posts 59 Credits 128
Completely stunned...
Floor11 wudixin96 Posted 2007-09-24 12:15
银牌会员 Posts 931 Credits 1,928
That call has a delaying effect, and many people use it in this forum.

See the convenience of the third-party tool?? o(∩_∩)o... Haha
Floor12 yoyodos Posted 2007-09-24 12:23
初级用户 Posts 59 Credits 128
I still need to study, haha. Let's ask another question. What's the function of the double percent signs below?
call set string=%%ln:"=#%%
Just came to the forum to hang out, thanks
Floor13 wudixin96 Posted 2007-09-24 12:25
银牌会员 Posts 931 Credits 1,928
It's just to play a delaying role in cooperation with call. Let's search first. My expression ability is very poor and I can't explain it clearly
Floor14 yoyodos Posted 2007-09-24 12:30
初级用户 Posts 59 Credits 128
Floor15 123cainiao Posted 2007-09-24 12:51
初级用户 Posts 41 Credits 97
Ask for advice, I have tested and found that the content display on the DOS screen has been modified, but the ab.txt file remains unchanged. That is, the new ab.txt file was not finally generated?
1 2 3 4  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023