CSV文件中的数据提取缺少某些数据

时间:2015-06-10 01:05:56

标签: csv batch-file batch-processing

我有一个脚本从CSV文件中提取数据并将其重新打印到另一个文件中,当与搜索字符串匹配时,从最后一条记录中删除额外的字段。见下文

echo off
setlocal EnableDelayedExpansion
pause
set cur=0
FOR /F "delims=" %%A in (INPUT.csv) DO (
set line=%%A
set line=!line:,,=, ,!

FOR /F "tokens=1-11 delims=," %%G in (^"!line!^") DO (
    if "%%G"=="" (echo.)
    if "%%G"==""FILENAME_YYYYMMDD.CSV"" (
        echo %%G,%%H,%%I,%%J >> output.csv
        goto EOF
    ) else (
        echo %%G,%%H,%%I,%%J,%%K,%%L,%%M,%%N,%%O,%%P,%%Q >> output.csv
    )
    set /a cur=cur+1
  )
)
:EOF
echo %cur%
pause

我的问题有两个方面。

  1. FILENAME_YYYYMMDD会根据创建输入文件的日期而更改。如何让它与FILENAME部分匹配?即。当%% G == FILENAME_20150610或FILENAME_20150611或FILENAME_XYZ
  2. 时%% G匹配
  3. 该脚本大多数有效,但许多记录缺少最后一个字段。共有7/190个记录缺少%% Q.这些不完整的记录随机分布在我的输出文件中。
  4. 以下示例:

    BEFORE

    "Parent","CODE1","Child ONE",CODEA,"COMPANY","","Address1",,"SUBURB","STATE","2000"
    "FILENAME_20150529.csv","20150529","15:09:30",187,"","","","","","",""
    

    AFTER

    "Parent","CODE1","Child ONE",CODEA,"COMPANY","","Address1", ,"SUBURB1","STATE2" 
    "FILENAME_20150529.csv","20150529","15:09:30",187
    

1 个答案:

答案 0 :(得分:0)

  1. 阅读help set并使用set = :~子字符串提取

    set fn=%%G
    set fn=!fn:~1,9!
    if /i !fn!==FILENAME_ (
    
  2. BAT解析(使用forset)不适合复杂的csv解析器,因为有关于逗号和引号的复杂规则。在您的情况下,您可能在字段内部有一些不平衡的引号或一些逗号,导致您的解析失败。