Reputation: 592
We have a list of log files, one for a day, named app_2020-04-11.log
, app_2020-04-12.log
etc. Now we want to get excerpts of these files, retrieving some lines of the original files, and store these lines in different files, one for each log file: logex_2020-04-11.log
, logex_2020-04-12.log
etc.
I can't find a solution how to output the search results in different files. I thought that the OutFile
-command could help me but I don't know how to set the Out-File Path correct with a variable.
Get-ChildItem app*.log |
Select-String -Pattern "findme here" |
Out-File -FilePath {$_.name -replace 'app','logex'}
How can I modify the out file name?
Upvotes: 0
Views: 92
Reputation: 26084
The following command should help you out.
We iterate over all app*.log
files, then we iterate over each line. If the line contains the pattern of interest (in this example find me here
), then we append that line to the renamed file:
Get-ChildItem app*.log |
ForEach-Object {
foreach($line in Get-Content $_.Name) {
if ($line -match "findme here"){
Add-Content -Path ($_.Name -replace "app", "logex") -Value $line
}
}
}
Upvotes: 1