Board logo

标题: [文本处理] 关于bat读取文本并截取内容的方法 [打印本页]

作者: leey84    时间: 2014-3-4 15:32     标题: 关于bat读取文本并截取内容的方法

Hi,大伙,我有个问题在这里请教,烦请帮助.

假设本地有文本名 test1.txt
内容为:
one=1
two=2
three=3

另有文件名test2.txt
内容为:
one
two
three

我希望能读取 test1.txt 每行等于号后面的内容(即 1, 2, 3), 然后替换掉 test2.txt 里面的 one,two,three, 即最终 test2.txt的内容是:
1
2
3

烦请大伙帮助,谢谢了!
作者: DAIC    时间: 2014-3-4 15:48

  1. @echo off
  2. for /f "tokens=1-2 delims==" %%i in (test1.txt) do (
  3.     set _%%i=%%j
  4. )
  5. (for /f "tokens=1-2 delims==" %%i in (test2.txt) do (
  6.     if defined _%%i (
  7.         call echo,%%_%%i%%
  8.     ) else (
  9.         echo,%%i
  10.     )
  11. ))>test3.txt
复制代码

作者: leey84    时间: 2014-3-4 16:18

回复 2# DAIC


   
感谢 DAIC 的帮助.
另请教一下,若是 test2.txt 内容如下,该如何识别并替换呢!?
Haone
<two>
This is a three !
作者: DAIC    时间: 2014-3-4 17:30

回复 3# leey84
  1. @echo off
  2. for /f "tokens=1-2 delims==" %%i in (test1.txt) do (
  3.     sed -i "s/%%i/%%j/g" test2.txt
  4. )
复制代码

作者: leey84    时间: 2014-3-4 17:58

回复 4# DAIC


    非常感谢!




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2