根据特定条件Windows批处理文件提取特定行

时间:2016-06-23 13:39:30

标签: windows csv powershell batch-file cmd

我正在尝试编写一个简单的批处理文件来执行以下操作:

  • 读取文本文件的行;每行包含ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'
  • 从多个team文件中查找具有该条件的数据。 (每个团队可以有很多行)
  • 将结果复制到一个CSV文件中。
  • 循环直到队伍结束。

CSV个文件包含以下数据:

CSV

文本文件包含:

"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,

"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,

"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,

"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,

"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,

"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,

......

这是我尝试过的代码:

Team A

Team B

Team C

Team D

...

3 个答案:

答案 0 :(得分:2)

PowerShell中,您可以使用Import-Csv cmdlet。只需指定第一列的标头以使用属性名称启用访问,使用Select-Object选择它,最后使用Out-File cmdlet将其写入文件:

Import-Csv -Path 'your_source_path.csv' -Header "Team" | 
    select -ExpandProperty Team | 
    Out-File 'YourDestination.txt'

文件内容:

Team A
Team C
Team B
Team A
Team C
Team B

答案 1 :(得分:1)

如何根据指定的条件提取行?

使用以下批处理文件(test.cmd):

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%t in (team.txt) do (
  for /f "usebackq tokens=*" %%i in (`findstr /c:"%%t" *.csv`) do (
    set _line=%%i
    rem strip filenames from findstr output
    set _line=!_line:*:=%!
    echo !_line! >> "%%t".csv
  )
)
endlocal

注意:

  • team.txt包含团队名称
  • 如果特定团队名称有匹配的行,则会创建一个名为团队名称 .csv的输出csv文件。

示例用法和输出:

F:\test>type team.txt
Team A
Team B
Team C
Team D
F:\test>type *.csv

1.csv


"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,
"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,
"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,
"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,
"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,
"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,

2.csv


"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,
"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,
"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,
"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,
"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,
"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,

F:\test>test
processing Team A
processing Team B
processing Team C
processing Team D

F:\test>type t*.csv

Team A.csv


"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,
"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,
"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,
"Team A",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,

Team B.csv


"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,
"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,
"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,
"Team B",2014/06/20 03:54:12 -0321,"Y","LQ4STREWNBKM5",004,

Team C.csv


"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,
"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,
"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,
"Team C",2011/01/14 00:00:00 -0400,"Y","LQ4STREWNBKM5",004,

进一步阅读

答案 2 :(得分:0)

假设你的文本文件没有每隔一行都有空格,因为这很奇怪:

for /f "delims=" %t in (teams-in.txt) do (grep "%t" *.csv > "%t.out")

grep是grep for Windows

[编辑:不要在与搜索* .csv 咳嗽相同的文件夹中输出.csv]