简体   繁体   中英

Batch For Loop Splitting on Tab Delimiter

I am attempting to write a batch script that uses a for loop to read in lines from an input file. I want to split each line that is read in on a tab delimiter. The input file is formatted as shown below with a tab between sourcefile and version. There is also an endline character after version.

sourcefile1   version1
sourcefile2   version2
sourcefile3   version3

I am running the following batch file.

@echo off
 for /F "tokens=1,2 delims=\T^" %%a in (input_file.txt) do ( 
   echo a = %%a 
   echo b = %%b 
)

My output is as follows:

a = sourcefile1 version1
b =
a = sourcefile2 version2
b =
a = sourcefile3 version3
b =

My desired output is:

a = sourcefile1
b = version1
a = sourcefile2
b = version2
a = sourcefile3
b = version3

Any input on how I can achieve my desired output? Thank you

您需要使用cmd /F:OFF制表符完成来启动 Cmd,然后使用"tokens=3 delims=^T" ,这里^T将是制表符文字。

Removing the delims options gave me my desired output

@echo off
for /F "tokens=1-2" %%a in (input_file.txt) do (
   echo %%a
   echo %%b
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM