批处理文件,用于提取列中的值符合条件的所有行

时间:2017-03-10 09:50:41

标签: batch-file

我尝试创建批处理文件以从CSV中提取所有行,其中列P等于以下3个字符串中的任何一个,保留标题。

漫游GST免费通话费

漫游GST免费短信费用

漫游GST免费GPRS费用

我不知道如何编写批处理文件,但我通常可以通过足够的谷歌搜索找到类似的东西然后摆弄它以获得我想要的东西,但这次我需要一些帮助。经过一堆搜索和摆弄后,我最终得到了以下内容(只是试图让其中一个标准工作),但它只提取标题。

@echo off &setlocal
set /p "header="<"report.csv"
>"reportroam.csv" echo.%header%
for /f "usebackq skip=1 delims=, tokens=1-16*" %%a in ("report.csv") do (
  if "%%p"=="Roaming GST-Free Call Charges" (
    >>"reportroam.csv" echo.%%a,%%b,%%c,%%d,%%e,%%f,,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s
  )
)

以下是我试图提取的一些例子。

28/01/2017,"9142321123",,"0497321123",,31/12/2016,"11:11:00","0000000000","FRANCE",,"00:00:00",0.000,"N",0.0000,"U-SMS","Roaming GST-Free SMS Charges",,,"No Hierarchy",,,,,,,,,,
28/01/2017,"9142321123",,"0497321123",,31/12/2016,"11:22:00","61452321123","FRANCE",,"00:01:00",0.000,"N",0.0000,"U-ROA","Roaming GST-Free Call Charges",,,"No Hierarchy",,,,,,,,,,

1 个答案:

答案 0 :(得分:2)

如果不存在包含所需字符串的其他列,那么为什么不使用FindStr

FindStr /I /C:"Roaming GST-Free Call Charges" /C:"Roaming GST-Free SMS Charges" /C:"Roaming GST-Free GPRS Charges" "report.csv">>"reportroam.csv"

您甚至可以拆分长线以使其看起来更好:

FindStr /I^
 /C:"Roaming GST-Free Call Charges"^
 /C:"Roaming GST-Free SMS Charges"^
 /C:"Roaming GST-Free GPRS Charges"^
 "report.csv">>"reportroam.csv"

修改
作为一个想法,如果您的标题中有一个已知的唯一字符串,您可以一次性完成所有操作:

@(FindStr /I^
 /C:"Unique Column Header String"^
 /C:"Roaming GST-Free Call Charges"^
 /C:"Roaming GST-Free SMS Charges"^
 /C:"Roaming GST-Free GPRS Charges"^
 "report.csv")>"reportroam.csv"