Board logo

标题: 等差数列批量替换文本中特定字段的问题。【已结】 [打印本页]

作者: yzthinker     时间: 2009-2-20 09:45    标题: 等差数列批量替换文本中特定字段的问题。【已结】
有一个文本文件a.txt如下: [00-1C-25-AA-D0-B6] ComputerName=学生机01 IP=192.168.2.11 [00-1C-25-AA-D0-CD] ComputerName=学生机02 IP=192.168.2.12 [00-1C-25-AA-D0-AC] ComputerName=学生机03 IP=192.168.2.13 [00-1C-25-AC-3B-DC] ComputerName=学生机04 IP=192.168.2.14 [00-1C-25-AA-D0-C2] ComputerName=学生机05 IP=192.168.2.15 [00-1C-25-AB-EB-3E] ComputerName=学生机06 ... ... [00-1C-25-DA-63-09] ComputerName=学生机69 IP=192.168.2.79 想通过批处理修改成b.txt 如下 [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.2 ... ... 就是学生机号与ip对应起来。百思不得其解。望高手赐教。 [ Last edited by yzthinker on 2009-2-20 at 23:51 ]

作者: netbenton     时间: 2009-2-20 13:08    标题: 看这样行吗?
@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

作者: yzthinker     时间: 2009-2-20 14:59    标题: 谢谢,运行结果是这样的
[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?谢谢

作者: netbenton     时间: 2009-2-20 19:42
@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

作者: yzthinker     时间: 2009-2-20 21:39    标题: 不好意思,运行结果还是那样。
[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 是隔行修改的,也就说只有第一个修改成功了

作者: netbenton     时间: 2009-2-20 22:37    标题: 不好意思,变量双义了
@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,应该没有问题了。

作者: yzthinker     时间: 2009-2-20 22:52    标题: 谢谢啊
运行通过了,谢谢。你编程序很在行吧?批处理的编写也很注意格式的,容易阅读,虽然有些符号我还不明白,但看起来条理清晰,很是舒服。谢谢了。

作者: HAT     时间: 2009-2-20 23:27    标题: Re 7楼
如果有人帮你解决了问题,建议进行以下操作: 1、修改帖子标题,在标题最前面注明:[已结] 2、给正确回答问题者加分(你自己的积分不会减少)以示感谢,虽然大家来论坛是为了学习而不是为了积分,但加分聊表谢意总是或多或少能够让花费了时间和精力无私帮助你的人略感欣慰。 3、如果你是在别人的提示下自己写出了代码,请把自己的方法贴出来跟大家分享一下,也许他人可以帮你指出代码中的不足之处、告诉你如何提高执行效率等,有交流才能更快地进步。

作者: yzthinker     时间: 2009-2-20 23:54    标题: (我是新手,不知道规矩。)遵命!
下不为例,谢谢斑竹提醒!
Originally posted by HAT at 2009-2-20 11:27 PM: 如果有人帮你解决了问题,建议进行以下操作: 1、修改帖子标题,在标题最前面注明:[已结] 2、给正确回答问题者加分(你自己的积分不会减少 ...

作者: terse     时间: 2009-2-21 00:00
针对楼主问题 可以简化下
@echo off&setlocal enabledelayedexpansion
(for /f "delims=" %%i in (ip.txt) do (
    set str=%%i  
    if /i "!str:~,3!"=="ip=" (set /a n+=1&call echo %%~ni.!n!) else echo %%i
  ))>new_ip.txt
pause

作者: yzthinker     时间: 2009-2-21 10:46    标题: 运行通过,真的很简洁哦。
谢谢啊,高手如云啊,受益匪浅。