Windows命令行 - 过滤具有非零值的行

时间:2015-06-11 12:24:05

标签: windows command-line cmd

我有一个file.data,其内容如下:

140919071513,10,0,1,0,0
140919071513,11,0,1,0,0
140919071513,12,67,1,0,0
140919071513,13,89,1,0,0
140919071513,14,0,1,0,0
140919071513,15,32,1,0,0
140919071513,16,0,1,0,0
140919071513,17,0,1,0,0
140919071513,18,78,1,0,0
140919071513,19,0,1,0,0
140919071513,20,34,1,0,0

我需要在Windows-DOS中运行单行命令以获得以下输出:

(第3列中的非零值)

140919071513,12,67,1,0,0
140919071513,13,89,1,0,0
140919071513,15,32,1,0,0
140919071513,18,78,1,0,0
140919071513,20,34,1,0,0

我使用此命令尝试获取某些内容,但无法获得所需的结果。

for /f "tokens=* delims=," %i in file.data do echo %i

谢谢

3 个答案:

答案 0 :(得分:2)

仅适用于手头有Powershell的情况:

Import-Csv .\file.data -Header "A","B","C","D","E","F"|where {$_.C -ne 0}|foreach-object {Write-host("$($_.A),$($_.B),$($_.C),$($_.D),$($_.E),$($_.F)")}

" foreach-object的最后一部分{Write-Host ..."可能会被优化,但它适用于这种情况。

答案 1 :(得分:2)

findstr /v /r /c:"^[^,]*,[^,]*,0," file.data

这将列出与正则表达式(/v)不匹配的所有行(/r):从行的开头(^)任何零或更多的序列不包含逗号([^,]*)的字符,后跟逗号,任何零个或多个字符的序列,不包含逗号,逗号,零和逗号。

答案 2 :(得分:2)

在cmd.exe中,您可以执行以下操作:

for /f "tokens=1-3,* delims=," %i in (tst.txt) do @if %k GTR 0 echo %i,%j,%k,%l 

几乎,你尝试了什么,但有一些变化:

... do @if ..// The @ will disable echo of the command itself ...
tokens=1-3,* // the first three tokens will be translated into variables
delims=,     // %i, %j and %k. The rest of each line ends up in %l

%k GTR 0     // GTR="greater", is a comparison of the third column against 0

这会让你

140919071513,12,67,1,0,0
140919071513,13,89,1,0,0
140919071513,15,32,1,0,0
140919071513,18,78,1,0,0
140919071513,20,34,1,0,0