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-03 09:55
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question for the experts. Please help, everyone. View 1,173 Replies 5
Original Poster Posted 2006-09-14 09:14 ·  中国 北京 朝阳区 联通
新手上路
Credits 11
Posts 1
Joined 2006-09-09 19:54
19-year member
UID 62242
Status Offline
1 .txt file contains
abc 1234 def
2.txt file contains
efg ?

How can I replace the ? in 2.txt with the 1234 from 1.txt so that 2.txt becomes
efg 1234

The 1234 here is a variable..


I hope everyone can solve this difficult problem that has bothered me for more than half a year. I'd be extremely grateful.
Floor 2 Posted 2006-09-14 20:42 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
Actually, although the OP described the problem clearly, the conditions given are a bit too few. Just doing what you described in the post above is not hard, but it depends on what the contents of your 1.txt and 2.txt are.

@echo off&setlocal
for /f "tokens=*" %%a in (1.txt) do set aa=%%a
<nul set/p= %aa:~10,4%>>2.txt


The problem in the post above can be solved like this. I don't think the contents of your 1.txt and 2.txt are only this little, right???
我今后在论坛的目标就是做个超级坏人!!!
Floor 3 Posted 2006-09-14 23:50 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
There are many ways to do it. You can take the 1234 in 1.txt as a parameter for call to implement it.
Floor 4 Posted 2006-09-15 02:46 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2007-02-02 20:20 ·  美国 北达科他州立大学
中级用户
★★
Credits 316
Posts 152
Joined 2006-06-18 13:01
20-year member
UID 57204
Gender Male
Status Offline
Here I will only deal with the text format you gave: 1234 is a variable, the length of the variable can change, and the number of spaces before and after the variable can also change


@echo off
sed "1s/abc*\(.*\)*def/\1/" 1.txt> temp1.txt
sed "/?/r temp1.txt" 2.txt|sed "1N;s/?.*\n\(.*\)/\1/"> temp2.txt
del temp1.txt
type temp2.txt
pause

[ Last edited by amao on 2007-2-3 at 02:33 AM ]
Floor 6 Posted 2007-02-03 08:39 ·  中国 内蒙古 呼和浩特 联通
新手上路
Credits 7
Posts 4
Joined 2007-02-03 01:15
19-year member
UID 78457
Gender Male
Status Offline
Not bad, learned something.
Forum Jump: