当 delim 为 ',' 时,批处理脚本跳过 .CSV 中的空白条目 [英] Batch script skipping blank entries in a .CSV when delim is ','
问题描述
我有一个 .CSV 文件,我试图对其进行排序以从数据创建另一个文件,但是当我运行它时,它会跳过空白条目.例如,如果一条线是值,值,值,,,值
我试图得到第 4 列,它会吐出第 6 列.大概是因为它是下一个有效值.我不希望它跳过空白条目,因为它会弄乱我正在尝试制作的表格.有谁知道如何解决这个问题?(欢迎任何提示,因为我很擅长批处理脚本)
I have a .CSV that I am trying to sort through to create another file from the data, but when I run it through, it skips blank entries. For example, if a line is
value,value,value,,,value
and I try to get the 4th column, it would spit out 6th. Presumably because it is the next valid value. I don't want it to skip the blank entry as it can mess up the tables I'm trying to make. Anyone know how to resolve this? (Any tips are welcome as I suck at batch scripts)
这是我的脚本:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14 delims=," %%a in (file.csv) DO (
echo %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n
)
pause
推荐答案
这是 FOR/F 循环的标准行为,连续分隔符仅用作一个分隔符.
但是您可以使用带有第二个 FOR/F 的解决方法.
用另一个字符作为每一列的前缀,在分隔符处拆分行并删除前缀.
This is the standard behaviour of the FOR/F loop, consecutive delims only used as one delimiter.
But you can use a workaround with a second FOR/F.
Prefix each column with another character, split the line at the delim and remove the prefix.
setlocal EnableDelayedExpansion
FOR /F "delims=" %%L in (test.bat) DO (
set "line=%%L,,,,,,,,"
set "line=#!line:,=,#!"
FOR /F "tokens=1,2,3,4 delims=," %%a in ("!line!") DO (
set "param1=%%a"
set "param2=%%b"
set "param3=%%c"
set "param4=%%d"
set "param1=!param1:~1!"
set "param2=!param2:~1!"
set "param3=!param3:~1!"
set "param4=!param4:~1!"
echo !param1! !param2! !param3! !param4!
)
)
这篇关于当 delim 为 ',' 时,批处理脚本跳过 .CSV 中的空白条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!