如何批处理或vbs以下CSV删除某些字符?

时间:2019-07-05 09:10:56

标签: regex csv batch-file

我正在设置一些文件以输出到木材加工CNC,这需要CAD的输出符合其格式。

我尝试在其上创建一个Excel宏,但操作员发现使用它令人困惑。

CSV需要ABS 1仅是1,而CAM需要删除_1和_2(最后2个字符)的文件名。

请提前欣赏! CSV(按附加代码)。

"CAM, file name",Length ,Width,Thickness,Material,Count,Edge Left,Edge Right,Edge Top,Edge Bottom,Barcode,Grain
,800.00,418.00,1650.00,,1,,,,,,
DOOR_LH_13_1,395.50,778.00,18.00,white,1,"ABS     1","ABS     1","ABS     1","ABS     1",279162,
DOOR_RH_14_1,395.50,778.00,18.00,white,1,"ABS     1","ABS     1","ABS     1","ABS     1",279164,
bottom_front_5_1,764.00,100.00,18.00,white,1,"ABS     1",,,"ABS     1",279148,
top_4_1,764.00,400.00,18.00,white,1,"ABS     1",,"ABS     1","ABS     1",279146,
left_side_1_1,1650.00,400.00,18.00,white,1,"ABS     1","ABS     1","ABS     1","ABS     1",279140,
right_side_2_1,1650.00,400.00,18.00,white,1,"ABS     1","ABS     1","ABS     1","ABS     1",279142,
Divider_8_1,374.00,352.00,18.00,white,1,,,"ABS     1",,279154,
Shelf_11_1,764.00,352.00,18.00,white,1,,,"ABS     1",,279158,
Shelf_12_1,764.00,352.00,18.00,white,1,,,"ABS     1",,279160,
Bottom_3_1,764.00,382.00,18.00,white,1,,,"ABS     1",,279144,
shelf_6_1,764.00,382.00,18.00,white,1,,,"ABS     1",,279150,
shelf_10_1,764.00,382.00,18.00,white,1,,,"ABS     1",,279156,
back_7_1,1632.00,764.00,18.00,white,1,,,,,279152,

1 个答案:

答案 0 :(得分:0)

批量处理时,通常使用for /f
处理csv 在逗号处分割,但是for /f将相邻的定界符视为一个。

所以我建议使用带有正则表达式的脚本语言,例如PowerShell。

(Get-Content .\SO_56900009.csv) -replace '"ABS *1"','1' -replace '(?<=_\d+)_\d'|Set-Content '.\New.csv'

> Get-Content .\New.csv
"CAM, file name",Length ,Width,Thickness,Material,Count,Edge Left,Edge Right,Edge Top,Edge Bottom,Barcode,Grain
,800.00,418.00,1650.00,,1,,,,,,
DOOR_LH_13,395.50,778.00,18.00,white,1,1,1,1,1,279162,
DOOR_RH_14,395.50,778.00,18.00,white,1,1,1,1,1,279164,
bottom_front_5,764.00,100.00,18.00,white,1,1,,,1,279148,
top_4,764.00,400.00,18.00,white,1,1,,1,1,279146,
left_side_1,1650.00,400.00,18.00,white,1,1,1,1,1,279140,
right_side_2,1650.00,400.00,18.00,white,1,1,1,1,1,279142,
Divider_8,374.00,352.00,18.00,white,1,,,1,,279154,
Shelf_11,764.00,352.00,18.00,white,1,,,1,,279158,
Shelf_12,764.00,352.00,18.00,white,1,,,1,,279160,
Bottom_3,764.00,382.00,18.00,white,1,,,1,,279144,
shelf_6,764.00,382.00,18.00,white,1,,,1,,279150,
shelf_10,764.00,382.00,18.00,white,1,,,1,,279156,
back_7,1632.00,764.00,18.00,white,1,,,,,279152,

要成为话题,您可以将一个衬纸包装在批处理文件中:

powershell -NoP -C "(Get-Content .\SO_56900009.csv) -replace '\"ABS *1\"','1' -replace '(?<=_\d+)_\d'|Set-Content '.\New.csv'"