I want to modify the following output from Select-String
\\path-to-logfile\copy2xl.log:390:NOTE: At least one W.D format was too small for the number to be printed. The decimal may be shifted by the "BEST" format.
So that linenumber "390" is connected to Path in parenthesis as below.
\\path-to-logfile\copy2xl.log(390):NOTE: At least one W.D format was too small for the number to be printed. The decimal may be shifted by the "BEST" format.
The default output you see comes from the MatchInfo.ToEmphasizedString(String)
Method, you can construct your own via string formatting, for example:
Select-String -Path somefile.ext -Pattern somePattern |
ForEach-Object { '{0}({1}):{2}' -f $_.Filename, $_.LineNumber, $_.Line }
$_.Filename
can be changed to $_.Path
there if you want the absolute path instead of the file name.
Another option, if you want the cmdlet to always display it's default output like the above is to use Update-FormatData
. First you export the format to a file:
Get-FormatData Microsoft.PowerShell.Commands.MatchInfo |
Export-FormatData -Path myCustomSelectString.Format.ps1xml
Then open that file and update the <ScriptBlock>
tag:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>MatchInfo</Name>
<ViewSelectedBy>
<TypeName>Microsoft.PowerShell.Commands.MatchInfo</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
'{0}({1}):{2}' -f $_.Path, $_.LineNumber, $_.Line
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
</Configuration>
And, lastly, Update-FormatData
:
Update-FormatData -PrependPath .\myCustomSelectString.Format.ps1xml