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-26 22:34
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Replacement of conditional text files [Answered] View 3,073 Replies 45
Floor 16 Posted 2007-09-24 13:00 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
It's very easy. Just change "echo variable" to "echo to the file".
Floor 17 Posted 2007-09-24 13:02 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by yoyodos at 2007-9-24 01:00 PM:
It's easy, just change echo variable to echo to file.


 Hehe, thank you. I understand.
Floor 18 Posted 2007-09-24 13:08 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
Let's study together.. Haha
Floor 19 Posted 2007-09-24 15:01 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
I also post one, haha

@echo off
setlocal enabledelayedexpansion
cd.>tmp.txt
set /a x=0

for /f "tokens=1 delims=姓名," %%r in (ab.txt) do echo %%r>>tmp.txt
for /f "delims=" %%a in (ab.txt) do (
set /a x+=1
set str=%%a
for /f "tokens=1* delims=:" %%h in ('findstr /n "." tmp.txt') do (
set n1=%%h
if !n1!==!x! (
set replaced=%%i
)
)
for /f "delims=" %%m in ('findstr /n "." cd.txt') do (
set n2=%%m
if !n2!==!x! (
set new="%%n"
)
)
call :replace
)
del /q tmp.txt
pause

:replace
set str=!str:%replaced%=%new%!
echo !str!


[ Last edited by yoyodos on 2007-9-24 at 03:02 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
123cainiao +2 2007-09-24 15:51
Floor 20 Posted 2007-09-24 15:51 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by yoyodos at 2007-9-24 03:01 PM:
I'll post one too, haha

@echo off
setlocal enabledelayedexpansion
cd.>tmp.txt
set /a x=0

for /f "tokens=1 delims=姓名," %%r in (ab.txt) do echo %%r>>tmp.txt
fo ...



Generally speaking, there are mainly 2 key points worthy of learning
1. The usage of "tokens=1 delims=", this point is very easy to get wrong many times
2. Variable replacement and extraction

If the content of ab.txt is changed from the original:
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
........


To the following:
abcd rster_www_er Name "Zhang San", ID '102', tel '0102211', 0, 25, 11
abcd rster_www_er Name "Li Sida", ID '102', tel '0202211', 0, 25, 11
abcd rster_www_er Name "Wang Er", ID '102', tel '01023311', 0, 25, 11
....


Then the above reference answer codes all need to be appropriately modified.
From this, it can be known that if you are editing a highly universal code, it is best to use the method of fuzzy search and positioning. For example, in this case, even if the length of each line of the ab.txt file and other contents change, as long as there is a field like , it can be found and replaced.
Can this be achieved?
Floor 21 Posted 2007-09-24 16:10 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
I don't quite understand what you mean.
The idea is to replace "姓名" in each line of the string with "abcd rster_www_er 姓 名", right?
Hahaha
Floor 22 Posted 2007-09-24 16:40 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by yoyodos at 2007-9-24 04:10 PM:
I don't quite understand what you mean.
The idea is to replace "Name" in each line of the string with "abcd rster_www_er 姓 名".
Is that okay?
Haha

No, what I mean is that as a highly versatile search statement, using fuzzy positioning to find is more practical than using the method of finding according to the delimiter.
For example, the content in the original ab.txt file:
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
This is very neat.
If it is not neat, it becomes:
abcd rster_www_er 姓 名 "Zhang San", ID '102', tel '0102211', 0, 25, 11
abcd, er 姓 名 "Li Sida", ID '102', tel '0202211', 0, 25, 11
abcd rster, www, r 姓 名 "Wang Er", ID '102', tel '01023311', 0, 25, 11
With such a format, the design idea of strictly finding according to the delimiter is almost unusable, and only fuzzy finding can be implemented.
In general, no matter how each line of text becomes, can be replaced in any different position in each line.

[ Last edited by 123cainiao on 2007-9-24 at 04:43 PM ]
Floor 23 Posted 2007-09-24 17:18 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
Oh, just use the name as the delimiter, right? Write the names after delims=, like 姓 名, 姓 名... and you can get the following names.
Floor 24 Posted 2007-09-24 18:49 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by yoyodos at 2007-9-24 05:18 PM:
Oh, just use the name as a delimiter.
Write the name after delims=, such as 姓 名, 姓 名..., and you can get the subsequent names.

Thank you for your answer. After repeated testing and appropriate adjustments, it works.
Your code has very good applicability. Thanks again!
Floor 25 Posted 2007-09-24 19:13 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
I also come to learn
@echo off
setlocal enabledelayedexpansion
set mn=0
for /f "tokens=1* delims=," %%i in (ab.txt) do (
set data=%%i%%j
set /a mn+=1
for /f "tokens=1* delims=:" %%a in ('findstr /n .* cd.txt') do (
if !mn! EQU %%a set data=!data:%%i=姓名"%%b",!
)
echo !data!>>c12.txt
)
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
123cainiao +2 2007-09-25 16:32
Floor 26 Posted 2007-09-25 13:25 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by terse at 2007-9-24 07:13 PM:
I'll also contribute a section for learning
@echo off
setlocal enabledelayedexpansion
set mn=0
for /f "tokens=1* delims=," %%i in (ab.txt) do (
set data=%%i%%j
set /a mn+=1
for ...

Among all the previous responses, terse's one has the strongest adaptability and can be fully adapted to the search and replacement of general-purpose files without modification. Only the "name" field needs to be replaced with what you want to search for.
Thank you, terse
Floor 27 Posted 2007-09-25 13:43 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
If we extend the problem, for example, the ab.txt file has only this line:
Name "Zhang San", ID '102', tel '0102211', 0, 25, 11
Now the cd.txt file remains unchanged:
Zhao Kuo
Wang Bin
Li Ling
......
Can we replace the name after the first line in the ab.txt file and then directly copy this format to generate a file ef.txt that has exactly the same number of lines as the cd.txt file:
Name "Zhao Kuo", ID '102', tel '0102211', 0, 25, 11
Name "Wang Bin", ID '102', tel '0102211', 0, 25, 11
Name "Li Ling", ID '102', tel '0102211', 0, 25, 11
........
Here the problem is not just about replacing corresponding lines, but also involves copying, which I think will be more complicated.
Floor 28 Posted 2007-09-25 14:23 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1* delims=," %%i in (ab.txt) do (
set data=%%j
)
for /f "tokens=1* delims=:" %%a in (cd.txt) do (
>>c12.txt echo 姓名"%%a",!data!
)
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
123cainiao +2 2007-09-25 16:31
Floor 29 Posted 2007-09-25 14:29 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
Terse answer is very good, ha, does 123cainiao still have questions? I also practice
Floor 30 Posted 2007-09-25 16:37 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by yoyodos at 2007-9-25 02:29 PM:
Terse's answer is very good, haha, does 123cainiao still have any questions?? I'll also practice


Hehe, thank you all for your answers. Different program expressions achieve the same goal, which is worth studying carefully.
   Best wishes to everyone for a happy Mid-Autumn Festival and a happy family!
Forum Jump: