我正在尝试编写一个简单的批处理文件来执行以下操作:
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
...
答案 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
包含团队名称。示例用法和输出:
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]