如何根据带标题的第二列值将CSV文件拆分为多个文件(不使用AWK)

时间:2017-04-28 09:01:37

标签: csv batch-file

我需要能够运行.bat文件来查看此CSV,并根据Col C - 客户代码中的值拆分为多个CSV,并在文件中生成带有标题的每个文件。

例如

 Invoice,PORef,CustomerCode,CustomerName,DocumentDate,ProductCode,ProductName
 111222,PO123,C100000,Test Store,1/1/15,AB1000,Test Soft Toy
 111222,PO123,C100000,Test Store,1/1/15,AB1002,Test Soft Toy2
 111222,PO321,C111000,My Store,1/10/15,AB1000,Test Soft Toy
 111222,PO123,C100000,Test Store,1/1/15,AB1001,Test Soft Toy1     
 111222,PO321,C111000,My Store,1/10/15,AB1001,Test Soft Toy1
 111222,PO321,C111000,My Store,1/10/15,AB1002,Test Soft Toy2

.bat应该运行并将CSV拆分为2个单独的CSV:

CSV1:

 Invoice,PORef,CustomerCode,CustomerName,DocumentDate,ProductCode,ProductName
 111222,PO123,C100000,Test Store,1/1/15,AB1000,Test Soft Toy
 111222,PO123,C100000,Test Store,1/1/15,AB1001,Test Soft Toy1
 111222,PO123,C100000,Test Store,1/1/15,AB1002,Test Soft Toy2

CSV2:

 Invoice,PORef,CustomerCode,CustomerName,DocumentDate,ProductCode,ProductName
 111222,PO321,C111000,My Store,1/10/15,AB1000,Test Soft Toy
 111222,PO321,C111000,My Store,1/10/15,AB1001,Test Soft Toy1
 111222,PO321,C111000,My Store,1/10/15,AB1002,Test Soft Toy2

以下是我尝试从互联网上搜索的代码,但我仍然面临打印输出文件标题的问题,我不明白如何检查C列的值

 @echo off
 set file=invoice.csv

 REM get header:
 set /p header=<%header%\invoice.csv

 REM process file line by line (ignore header):
 for /f "skip=1 tokens=,* delims=," %%a in (%file%) do (
  set line=%%1
   if not exist "%%a.csv" echo %header%>"%%a.csv"
   echo %%a,%%b>>"%%a.csv"
 )

感谢您的帮助^^

2 个答案:

答案 0 :(得分:0)

"entities": [
  {
    keyName:'TLSM01',
    data: [
      {
        "01": {
          "Name": "Light",
          "Properties": [
            {
              "state": [
                {
                  "type": "boolean",
                  "propertyMode": "actuator"
                }
              ],
              "brightness": [
                {
                  "type": "integer",
                  "propertyMode": "actuator"
                }
              ]
            }
          ]
        }
      }
    ]
  }
]

答案 1 :(得分:0)

假设CSV数据中的任何字段(或单元格值)都没有自己的逗号,(当值包含在""中时,它在CSV中有效),您可以使用单个findstr command行来过滤所需的信息,甚至包括标题行。要将提取的数据写入文件,请使用output redirection >。以下是一些示例代码:

findstr /I /R %= use regular epression search; remove `/I` for case-sensitive search =% ^
    /C:"^[^,]*,[^,]*,CustomerCode," %= third column header; omit to skip header =% ^
    /C:"^[^,]*,[^,]*,C100000,"      %= third column value; there may be more =% ^
    "invoice.csv" > "invoice_C100000.csv"
findstr /I /R %= use regular epression search; remove `/I` for case-sensitive search =% ^
    /C:"^[^,]*,[^,]*,CustomerCode," %= third column header; omit to skip header =% ^
    /C:"^[^,]*,[^,]*,C111000,"      %= third column value; there may be more =% ^
    "invoice.csv" > "invoice_C111000.csv"

或者写成一行,每行都没有任何内联评论:

findstr /I /R /C:"^[^,]*,[^,]*,CustomerCode," /C:"^[^,]*,[^,]*,C100000," "invoice.csv" > "invoice_C100000.csv"
findstr /I /R /C:"^[^,]*,[^,]*,CustomerCode," /C:"^[^,]*,[^,]*,C111000," "invoice.csv" > "invoice_C111000.csv"

使用的搜索字符串(或正则表达式)如^[^,]*,[^,]*,CustomerCode,表示:

  • ^ - 锚点匹配行的开头;
  • 两次相同的序列:
    • [^,] - 定义逆字符类并匹配除,之外的任何字符;
    • * - 匹配前面的表达式零次或多次;
    • , - 匹配文字逗号,;
  • CustomerCode - 与列标题匹配的搜索字符串的文字部分(或列值C100000C111000);
  • , - 匹配文字逗号;
  • 不考虑剩余的行字符串;