![]() |
中国DOS联盟-- 联合DOS 推动DOS 发展DOS --联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum |
| 游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟 |
|
中国DOS联盟论坛 现在时间是 2026-08-11 09:46 |
共 47,811 主题排行 / 349,897 发帖 / 今日 0 篇 / 48,256 会员排行 |
| DOS批处理 & 脚本技术(批处理室) » 等差数列批量替换文本中特定字段的问题。【已结】 |
| 可打印版本 812 / 10 |
| 第1楼 yzthinker | 发表于 2009-02-20 09:45 |
| 新手上路 发帖 13 积分 17 | |
|
等差数列批量替换文本中特定字段的问题。【已结】 有一个文本文件a.txt如下:
ComputerName=学生机01 IP=192.168.2.11 ComputerName=学生机02 IP=192.168.2.12 ComputerName=学生机03 IP=192.168.2.13 ComputerName=学生机04 IP=192.168.2.14 ComputerName=学生机05 IP=192.168.2.15 ComputerName=学生机06 ... ... ComputerName=学生机69 IP=192.168.2.79 想通过批处理修改成b.txt 如下 ComputerName=学生机01 IP=192.168.2.1 ComputerName=学生机02 IP=192.168.2.2 ... ... 就是学生机号与ip对应起来。百思不得其解。望高手赐教。 [ Last edited by yzthinker on 2009-2-20 at 23:51 ] |
|
| 第2楼 netbenton | 发表于 2009-02-20 13:08 |
| 银牌会员 发帖 752 积分 1,916 来自 广西 | |
|
看这样行吗? @echo off
set n=10 ::设置IP地址最后一位要减的值 for /f "delims=" %%a in (a.txt) do (set "str=%%a"&call :sub %%a) goto :eof :sub ::不是ip行不处理 if /i not "%1"=="ip" goto :nochange set ip=%2 ::点号替换为空格 set ip=%ip:.= % call :sub2 %ip% ::替换修改后的结果 call set str=%%str:%2=%ip%%% :nochange echo %str% goto :eof :sub2 ::第四位减n,再重组 set/a n=%4-n set ip=%1.%2.%3.%n% goto :eof |
|
| 第3楼 yzthinker | 发表于 2009-02-20 14:59 |
| 新手上路 发帖 13 积分 17 | |
|
谢谢,运行结果是这样的 [00-1C-25-AA-D0-B6]
ComputerName=学生机01 IP=192.168.2.1 [00-1C-25-AA-D0-CD] ComputerName=学生机02 IP=192.168.2.11 [00-1C-25-AA-D0-AC] ComputerName=学生机03 IP=192.168.2.2 [00-1C-25-AC-3B-DC] ComputerName=学生机04 IP=192.168.2.12 [00-1C-25-AA-D0-C2] ComputerName=学生机05 IP=192.168.2.3 [00-1C-25-AB-EB-3E] ComputerName=学生机06 IP=192.168.2.13 [00-1C-25-AA-D0-B4] ComputerName=学生机07 IP=192.168.2.4 [00-1C-25-A9-EC-34] ComputerName=学生机08 IP=192.168.2.14 [00-1C-25-AA-D0-8C] ComputerName=学生机09 IP=192.168.2.5 [00-1C-25-AA-D0-B9] ComputerName=学生机10 IP=192.168.2.15 [00-1C-25-DA-62-F4] ComputerName=学生机11 IP=192.168.2.6 [00-1C-25-DA-63-0D] ComputerName=学生机12 IP=192.168.2.16 [00-1C-25-DA-63-1D] ComputerName=学生机13 IP=192.168.2.7 [00-1C-25-AB-EB-44] ComputerName=学生机14 IP=192.168.2.17 [00-1C-25-DA-63-0B] ComputerName=学生机15 IP=192.168.2.8 [00-1C-25-AB-EB-2B] ComputerName=学生机16 IP=192.168.2.18 [00-1C-25-AB-EB-AB] ComputerName=学生机17 IP=192.168.2.9 [00-1C-25-A9-EC-54] ComputerName=学生机18 IP=192.168.2.19 [00-1C-25-AB-EB-26] ComputerName=学生机19 IP=192.168.2.10 [00-1C-25-DA-63-05] ComputerName=学生机20 IP=192.168.2.20 [00-1C-25-AC-3C-62] ComputerName=学生机21 IP=192.168.2.11 另外怎样生成b.txt?谢谢 |
|
| 第4楼 netbenton | 发表于 2009-02-20 19:42 |
| 银牌会员 发帖 752 积分 1,916 来自 广西 | |
|
@echo off
set n=10 ::设置IP地址最后一位要减的值 ::****改了这里就可以了 (for /f "delims=" %%a in (a.txt) do (set "str=%%a"&call :sub %%a))>b.txt ::**** goto :eof :sub ::不是ip行不处理 if /i not "%1"=="ip" goto :nochange set ip=%2 ::点号替换为空格 set ip=%ip:.= % call :sub2 %ip% ::替换修改后的结果 call set str=%%str:%2=%ip%%% :nochange echo %str% goto :eof :sub2 ::第四位减n,再重组 set/a n=%4-n set ip=%1.%2.%3.%n% goto :eof |
|
| 第5楼 yzthinker | 发表于 2009-02-20 21:39 |
| 新手上路 发帖 13 积分 17 | |
|
不好意思,运行结果还是那样。 [00-1C-25-AA-D0-B6]
ComputerName=学生机01 IP=192.168.2.1 [00-1C-25-AA-D0-CD] ComputerName=学生机02 IP=192.168.2.11 [00-1C-25-AA-D0-AC] ComputerName=学生机03 IP=192.168.2.2 [00-1C-25-AC-3B-DC] ComputerName=学生机04 IP=192.168.2.12 [00-1C-25-AA-D0-C2] ComputerName=学生机05 IP=192.168.2.3 [00-1C-25-AB-EB-3E] ComputerName=学生机06 IP=192.168.2.13 [00-1C-25-AA-D0-B4] ComputerName=学生机07 IP=192.168.2.4 是隔行修改的,也就说只有第一个修改成功了 |
|
| 第6楼 netbenton | 发表于 2009-02-20 22:37 |
| 银牌会员 发帖 752 积分 1,916 来自 广西 | |
|
不好意思,变量双义了 @echo off
set n=10 ::设置IP地址最后一位要减的值 (for /f "delims=" %%a in (a.txt) do (set "str=%%a"&call :sub %%a))>b.txt goto :eof :sub ::不是ip行不处理 if /i not "%1"=="ip" goto :nochange set ip=%2 ::点号替换为空格 set ip=%ip:.= % call :sub2 %ip% ::替换修改后的结果 call set str=%%str:%2=%ip%%% :nochange echo %str% goto :eof :sub2 ::第四位减n,再重组 set/a m=%4-n set ip=%1.%2.%3.%m% goto :eof 增加一个变量m,应该没有问题了。 |
|
| 第7楼 yzthinker | 发表于 2009-02-20 22:52 |
| 新手上路 发帖 13 积分 17 | |
|
谢谢啊 运行通过了,谢谢。你编程序很在行吧?批处理的编写也很注意格式的,容易阅读,虽然有些符号我还不明白,但看起来条理清晰,很是舒服。谢谢了。
![]() |
|
| 第8楼 HAT | 发表于 2009-02-20 23:27 |
| 版主 发帖 5,017 积分 9,023 | |
|
Re 7楼 如果有人帮你解决了问题,建议进行以下操作:
1、修改帖子标题,在标题最前面注明:[已结] 2、给正确回答问题者加分(你自己的积分不会减少)以示感谢,虽然大家来论坛是为了学习而不是为了积分,但加分聊表谢意总是或多或少能够让花费了时间和精力无私帮助你的人略感欣慰。 3、如果你是在别人的提示下自己写出了代码,请把自己的方法贴出来跟大家分享一下,也许他人可以帮你指出代码中的不足之处、告诉你如何提高执行效率等,有交流才能更快地进步。 |
|
| 第9楼 yzthinker | 发表于 2009-02-20 23:54 |
| 新手上路 发帖 13 积分 17 | |
|
(我是新手,不知道规矩。)遵命! 下不为例,谢谢斑竹提醒!
Originally posted by HAT at 2009-2-20 11:27 PM: |
|
| 第10楼 terse | 发表于 2009-02-21 00:00 |
| 银牌会员 发帖 946 积分 2,404 | |
|
针对楼主问题 可以简化下
|
|
| 第11楼 yzthinker | 发表于 2009-02-21 10:46 |
| 新手上路 发帖 13 积分 17 | |
|
运行通过,真的很简洁哦。 谢谢啊,高手如云啊,受益匪浅。
|
|
|
[ 联系联盟系统管理团队 -
中国DOS联盟 -
标准版 ] Sponsored by ifanr Inc | © 2001–2023 |