JustAGuy
JustAGuy

Reputation: 5961

Getting data from multiple files at the same time

Lets say I have 3 txt files with contents as followed:

1.txt

A
B
C

2.txt

D
E
F

3.txt

G
H
I

What I'd like to do is to read the same line from each of them like this:

A D G
B E H
C F I

How can it be done? Thanks

Upvotes: 1

Views: 159

Answers (2)

mjolinor
mjolinor

Reputation: 68341

Another solution:

$1,$2,$3 = 1..3|%{,(get-content "$_.txt")}
$1 | % {$i=0} {$_, $2[$i], $3[$i++] -join ' '}

Upvotes: 0

Joey
Joey

Reputation: 354874

If you want strings (would be a weird requirement for PowerShell, though) and each file has the same number of lines:

$1,$2,$3 = 1..3 | ForEach-Object { Get-Content $_.txt }
0..($1.Count) | ForEach-Object {
  $1[$_], $2[$_], $3[$_] -join ' '
}

Upvotes: 1

Related Questions