标题: [文本处理] 批处理怎样根据一个文件的内容查找另一个文件的指定列? [打印本页]
作者: lengguoyong 时间: 2011-7-26 08:35 标题: 批处理怎样根据一个文件的内容查找另一个文件的指定列?
有文件1.txt
-176.250000 89.750000
-175.750000 89.750000
-175.250000 89.750000
-174.750000 89.750000
-174.250000 89.750000
和文件2.txt
-179.750000 89.750000 0.936815
-179.250000 89.750000 0.936815
-178.750000 89.750000 0.937381
-178.250000 89.750000 0.937381
-177.750000 89.750000 0.936225
-177.250000 89.750000 0.936225
-176.750000 89.750000 0.925175
-176.250000 89.750000 0.925175
-175.750000 89.750000 0.930261
-175.250000 89.750000 0.930261
-174.750000 89.750000 0.927604
-174.250000 89.750000 0.927604
-173.750000 89.750000 0.915246
-173.250000 89.750000 0.915246
-172.750000 89.750000 0.914859
-172.250000 89.750000 0.914859
-171.750000 89.750000 0.912408
-171.250000 89.750000 0.912408
-170.750000 89.750000 0.914096
-170.250000 89.750000 0.914096
-169.750000 89.750000 0.916220
怎样根据1.txt中的每行数据找到2.txt中对应行的第三列数据并输出到3.txt啊。- @echo off
- For /F "tokens=1,2 delims= " %%i In (1.txt) do
- ( For /F "tokens=1,2,3 delims= " %%a In (2.txt) do ( if %%a%%b EQU %%i%%j echo %%c) )>>3.txt
- pause>nul
复制代码
我自己试写的代码运行不起来,请各位大侠看看怎么回事啊!!!
作者: bluewing009 时间: 2011-7-26 08:43
本帖最后由 bluewing009 于 2011-7-26 08:47 编辑
你好,请注意发帖格式与论坛规则的要求,建议楼主修改一下标题。
详情请见:
http://www.bathome.net/thread-963-1-1.html 第九条
http://www.bathome.net/thread-4054-1-1.html
关于你的问题,请参考findstr命令
作者: saogz 时间: 2011-7-27 09:40
本帖最后由 saogz 于 2011-7-27 09:44 编辑
- @echo off
- for /f "tokens=1,2* delims= " %%i in (1.txt) do (
- findstr /I /c:"%%i %%j" 2.txt >> .tmp
- )
-
- for /f "tokens=3 delims= " %%i in (.tmp) do (
- echo %%i >> 3.txt
- )
- del .tmp
- pause
复制代码
作者: saogz 时间: 2011-7-27 09:59
- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=1,2* delims= " %%i in (1.txt) do (
- for /f "tokens=3 delims= " %%a in ('findstr /I /c:"%%i %%j" 2.txt') do (
- echo %%a >> 3.txt
- )
- )
- pause
复制代码
作者: CUer 时间: 2011-7-30 23:31
回复 1# lengguoyong
你的代码稍微修改一下即可:- @echo off
- (for /f "delims=" %%i in (a.txt) do (
- for /f "tokens=1,2,3 delims= " %%a in (b.txt) do (
- if "%%a %%b" equ "%%i" (
- echo,%%c
- )
- )
- ))>c.txt
复制代码
作者: CUer 时间: 2011-7-30 23:32
回复 4# saogz
你的findstr不如楼主直接比较的效率高吧
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |