使用CMD / Batch从CSV删除列

时间:2018-11-23 09:37:07

标签: csv batch-file cmd

我目前从供应商处下载了一个CSV文件,其中包含我不需要的数据,然后再将其导入我们的系统。

我需要使用批处理文件从CSV中删除一些列,就像在citrix上一样,我们没有选择powershell。

没有列标题,下面是CSV的样子:

"9826XXXXXXXXXX217","60007834        ","    10.00","D","22/11/2018"
"9826XXXXXXXXXX324","60008504        ","    12.00","D","22/11/2018"
"9826XXXXXXXXXX414","60010605        ","    12.00","D","22/11/2018"
"9826XXXXXXXXXX013","60011385        ","    12.00","D","22/11/2018"

我需要输出为第二,第三,第五列。

"60007834        ","    10.00","22/11/2018"
"60008504        ","    12.00","22/11/2018"
"60010605        ","    12.00","22/11/2018"
"60011385        ","    12.00","22/11/2018"

2 个答案:

答案 0 :(得分:1)

真的不太难。

来自批处理文件,假设文件名为test.csv

(for /f "tokens=2,3,5 delims=," %%i in (test.csv) do echo %%i,%%j,%%k) > newcsv.csv

这将简单地将值2,3,5写入新的csv文件。

要从cmdline运行它,只需删除每个%

之一
(for /f "tokens=2,3,5 delims=," %i in (test.csv) do echo %i,%j,%k) > newcsv.csv

请注意,这假设您的值中的数据不包含,(如果包含),我们需要进行一些更改。

答案 1 :(得分:1)

如果字段内可能有逗号,
使用对带引用参数的子调用:

:: Q:\Test\2018\11\23\SO_53444017.cmd
@Echo off
(for /f "delims=" %%A in (test.csv) do Call :Split %%A
) > test_New.csv
goto :Eof
:Split
Echo(%2,%3,%5

> type test_New.csv
"60007834        ","    10.00","22/11/2018"
"60008504        ","    12.00","22/11/2018"
"60010605        ","    12.00","22/11/2018"
"60011385        ","    12.00","22/11/2018"

编辑:变体处理所有* .csv文件(将_New附加到名称上)

:: Q:\Test\2018\11\23\SO_53444017.cmd
@Echo off
For %%F in (*.csv) do ( 
    (for /f "delims=" %%A in (%%F) do Call :Split %%A
    ) > "X:\Path\%%~nF_New.csv"
)
goto :Eof
:Split
Echo(%2,%3,%5