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-27 00:07
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Replacement of conditional text files [Answered] View 3,078 Replies 45
Original Poster Posted 2007-09-23 23:21 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
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 ]
Floor 2 Posted 2007-09-24 11:03 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
It feels a bit difficult, ha, I'll take a look again
Floor 3 Posted 2007-09-24 11:09 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
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!
Floor 4 Posted 2007-09-24 11:12 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
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.
Floor 5 Posted 2007-09-24 11:41 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
```
@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%
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
123cainiao +2 2007-09-24 14:33
Floor 6 Posted 2007-09-24 11:52 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
Study! So powerful
Floor 7 Posted 2007-09-24 12:07 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Thank you very much, wudixin96. It's really eye-opening.
Floor 8 Posted 2007-09-24 12:11 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
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!
Floor 9 Posted 2007-09-24 12:12 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```gawk -F "BEGIN{OFS=\"\"}{if(FNR==NR){str=$0} else { a+=1; $2=\"\x22\" str \""\"; print $0}}" cd.txt ab.txt```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
123cainiao +2 2007-09-24 14:34
Floor 10 Posted 2007-09-24 12:14 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
Completely stunned...
Floor 11 Posted 2007-09-24 12:15 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
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
Floor 12 Posted 2007-09-24 12:23 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
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
Floor 13 Posted 2007-09-24 12:25 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
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
Floor 14 Posted 2007-09-24 12:30 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
Floor 15 Posted 2007-09-24 12:51 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
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?
Forum Jump: