批处理脚本跳过空白条目在.csv当DELIM是',' [英] Batch script skipping blank entries in a .CSV when delim is ','

查看:291
本文介绍了批处理脚本跳过空白条目在.csv当DELIM是','的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图通过排序,创建从数据的另一个文件.CSV,但是当我运行它通过,它跳过空白项。例如,如果线路
价值,价值,价值,,,值
我试图让第4列,它会吐出第6位。 presumably,因为它是一个有效的值。我不希望它跳过空白项,因为它可以搞砸我试图使表。任何人都知道如何解决这个问题? (任何提示,欢迎,因为我吸在批处理脚本)

下面是我的脚本:

  FOR / F令牌= 1,2,3,4,5,6,7,8,9,10,11,12,13,14 delims =%在(FILE.CSV)%一DO(
    呼应%%一个%% b %%ç%% d %%已Ë%%˚F%%摹%% ^ h %%我%%Ĵ%%ķ%%升%%米%% n和

暂停


解决方案

这是FOR / F环的标准行为,连续delims只能作为一个分隔符。结果
但是你可以用一种变通方法与第二F​​OR / F。结果
preFIX与另一个字符的每一列,在DELIM拆分行,并删除preFIX。

  SETLOCAL EnableDelayedExpansion
FOR / Fdelims =%% L的(test.bat的)DO(
    一套行= %%大号,,,,,,,,
    一套!行=#线:,=#!
    FOR / F令牌= 1,2,3,4 delims =%%一中(!行了!),DO(
        设置参数1 = %%一个
        设置参数2 = %% B
        设置参数3 = %% C
        设置param4 = %% D
        设置!参数1 =参数1:1〜!
        设置!参数2 =参数2:1〜!
        设置!参数3 =参数3:1〜!
        设置!param4 = param4:1〜!
        回音!参数1! !参数2! !参数3! !param4!
    )

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)

Here is my script:

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

解决方案

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!
    )
)

这篇关于批处理脚本跳过空白条目在.csv当DELIM是','的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆