Reputation: 45
I'm trying to write a simple batch file to do the following:
team
.CSV
files. (Each team could have many rows)CSV
file.CSV
files contains data like this:
"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,
......
The text file contains:
Team A
Team B
Team C
Team D
...
This is the code I tried:
FOR %%A IN (Team.txt) DO Find "%%A" *.csv > result.txt
Upvotes: 1
Views: 3460
Reputation: 7921
Use the following batch file (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
Notes:
team.txt
contains the team names.Example usage and output:
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,
Upvotes: 1
Reputation: 29033
Assuming your text file does not have spaces every other line, because that's just weird:
for /f "delims=" %t in (teams-in.txt) do (grep "%t" *.csv > "%t.out")
Where grep is grep for Windows
[edit: and don't output to .csv in the same folder as searching *.csv cough ]
Upvotes: 0
Reputation: 58991
In PowerShell
, you could use the Import-Csv cmdlet. Just specify the header for the first column to enable the access using the property name, select it using Select-Object and finally write it to a file using the Out-File cmdlet:
Import-Csv -Path 'your_source_path.csv' -Header "Team" |
select -ExpandProperty Team |
Out-File 'YourDestination.txt'
File content:
Team A
Team C
Team B
Team A
Team C
Team B
Upvotes: 2