如何编写脚本搜索传入的.CSV文件中的特定行

时间:2017-10-30 13:52:03

标签: csv batch-file

对我来说,每天搜索一次.csv文件夹中的所有C:\csvtest个文件的最佳方法是什么?

我只对标记名以.REG102结尾的行感兴趣,如果它是<=3000,如果是,则将其输出到同一位置的文本文件中。

e.g。 matchedfilename msg (IDG00105--IDG00105) 2017_10_30_13_30_33.csv 其中matchedfilename是找到成功条目的文件的名称。

.csv文件内容:

[Data]  
Tagname,TimeStamp,Value,DataQuality  
IDG00105--IDG00105.REG96,2017-10-30 13:30:33.000,6774,Good
IDG00105--IDG00105.REG97,2017-10-30 13:30:33.000,1,Good
IDG00105--IDG00105.REG98,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG99,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG100,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG101,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG102,2017-10-30 13:30:33.000,3452,Good
IDG00105--IDG00105.REG103,2017-10-30 13:30:33.000,52,Good
IDG00105--IDG00105.REG104,2017-10-30 13:30:33.000,0,Good 
IDG00105--IDG00105.REG105,2017-10-30 13:30:33.000,13824,Good
IDG00105--IDG00105.REG106,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG107,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG108,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG109,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG110,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG111,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG112,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG113,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG114,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG115,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG116,2017-10-30 13:30:33.000,6287,Good
IDG00105--IDG00105.REG117,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG118,2017-10-30 13:30:33.000,339,Good
IDG00105--IDG00105.REG119,2017-10-30 13:30:33.000,0,Good
IDG00105--IDG00105.REG120,2017-10-30 13:30:33.000,30629,Good

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用powershell:

powershell "import-csv data.csv| where {$_.Value -le 3000  -and $_.Value -gt 0}"

纯批次:

@echo off
setlocal enableDelayedExpansion
for /f "usebackq skip=1 tokens=1-4 delims=," %%a in ("data.csv") do (
    set /a value=%%c
    if !value! LEQ 3000 if !value! GTR 0 (
       (echo(%%a,%%b,%%c,%%d)
    )
)

答案 1 :(得分:0)

如果我已正确理解您的任务,可能会使用以下内容:

@Echo Off
SetLocal EnableDelayedExpansion

Set "sD=C:\csvtest"
Set "fM=*.csv"
Set "sM=.REG102,"
Set "mV=3000"
Set "oF=output.txt"

If /I Not "%CD%"=="%sD%" CD/D "%sD%" 2>Nul||Exit/B
If Not Exist "%fM%" Exit/B
::If Exist "%oF%" Del "%oF%"

For /F "Tokens=1*Delims=:" %%A In ('FindStr/L "%sM%" "%fM%"'
) Do For /F "Tokens=1-3Delims=," %%C In ("%%~B"
) Do If "%%E" LEq "%mV%" (Set "nV=%%~nC"&Set "tV=%%~nD"
    Set "tV=!tV:-=_!"&Set "tV=!tV::=_!"
    >>"%oF%" Echo %%~nA msg (!nV:*:=!^) !tV: =_!%%~xA)

您可以根据需要更改第4-8行的源目录,文件掩码,搜索掩码,最大值和输出文件。如果您希望每次脚本运行时都将新鲜内容写入输出文件,请从行12中删除前两个字符。