Parse tab delimited text file

感情迁移 提交于 2019-12-02 08:53:19

问题


I need to parse a tab-delimited text file by grabbing specific columns, like columns 1 and 5, and output each of these columns into a text file. Please find an example of the data file, and the code:

Data file:

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

Batch file:

@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

This works, however, the For loop stops on the first line.

Could you help me in finding the issue?


回答1:


Your code works fine for me, but maybe try this shortened version?

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



回答2:


While I would recommend using the shortened version by Bali C, the reason the code above might be stopping after the first line is because the goto :eof or exit command is not at the end of the :doSomething function.

@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

Or if Extensions are disabled, it would be outputting the following error message:

/F was unexpected at this time.

To resolve this add EnableExtensions to your setlocal command.

@echo off & setlocal EnableExtensions



回答3:


your code is working fine, check the output below.

Account.txt

"COL5"

"012"

"123"

""

Entity.txt

"COL1"

"123"

"234"

"etc"



来源:https://stackoverflow.com/questions/14250593/parse-tab-delimited-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!