解析制表符分隔的文本文件

时间:2013-01-10 03:55:00

标签: batch-file dos file-processing

我需要通过抓取特定列(如第1列和第5列)来解析制表符分隔的文本文件,并将每个列输出到文本文件中。请查找数据文件的示例和代码:

数据文件:

COL1 COL2 COL3 COL4 COL5 COL6
123  345  678  890  012  234
234  456  787  901  123  345
etc

批处理文件:

@echo off & setlocal 
For /F "tokens=1,5*" %%i in (myFile.dat) do call :doSomething "%%i" "%%j"
goto :eof 

:doSomething 
Set VAR1=%1
Set VAR2=%2
@echo %VAR1%>>Entity.txt
@echo %VAR2%>>Account.txt

但是,For循环在第一行停止。

你能帮我找到这个问题吗?

3 个答案:

答案 0 :(得分:3)

您的代码对我来说很好,但也许可以尝试这个缩短的版本?

@echo off
for /F "tokens=1,5*" %%i in (myFile.dat) do (
echo %%i >>Entity.txt
echo %%j >>Account.txt
)

答案 1 :(得分:1)

虽然我建议使用Bali C的缩短版本,但是上面的代码可能在第一行之后停止的原因是因为goto :eofexit命令是不在:doSomething函数的末尾。

@echo off & setlocal 
For /F "tokens=1,5*" %%i in (myFile.dat) do call :doSomething "%%i" "%%j"
goto :eof 

:doSomething 
Set VAR1=%1
Set VAR2=%2
@echo %VAR1%>>Entity.txt
@echo %VAR2%>>Account.txt
goto :eof

或者如果禁用扩展,则会输出以下错误消息:

/F was unexpected at this time.

要解决此问题,请将EnableExtensions添加到setlocal命令。

@echo off & setlocal EnableExtensions

答案 2 :(得分:0)

您的代码工作正常,请检查下面的输出。

<强> Account.txt

“COL5”

“012”

“123”

“”

<强> Entity.txt

“COL1”

“123”

“234”

“等等”