我有一个脚本从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
我的问题有两个方面。
以下示例:
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
答案 0 :(得分:0)
阅读help set
并使用set = :~
子字符串提取
set fn=%%G
set fn=!fn:~1,9!
if /i !fn!==FILENAME_ (
BAT解析(使用for
或set
)不适合复杂的csv解析器,因为有关于逗号和引号的复杂规则。在您的情况下,您可能在字段内部有一些不平衡的引号或一些逗号,导致您的解析失败。