Reputation: 125
I want to search files from the subfolder and grep row from the file. Text files have 3 rows, the expected result is grepping 2nd row. I hope the file name pass to Get-Content, but it failed.
Get-ChildItem -Name -Recurse -File -Include *.txt* | (Get-Content $_)[2]
Upvotes: 0
Views: 191
Reputation: 125
I understand Get-ChildItem is not String.
$f = (Get-ChildItem -Name -Recurse -File -Filter *.txt*) -as [string[]]
$i = 1
foreach($j in $f) {
#Write-Host $i : $j
Write-Host (Get-Content $j)[2]
$i++
}
Upvotes: 0
Reputation: 59779
What you are doing is almost fine, the parenthesis should be wrapping the whole expression and $_
should not be there:
(Get-ChildItem -Name -Recurse -File -Filter *.txt* | Get-Content)[2]
You could add -TotalCount 2
to Get-Content
so it only reads the first 2 lines however if the files only have 3 lines it wouldn't make much of a difference.
Upvotes: 1