当我使用select-string
时,我几乎总是希望输出对齐,即文件名,行号和找到的文本都应该对齐成列。在视觉上它更不容忽视,通常更容易发现差异。作为一个简单的例子,我在中间文件中注入了一个额外的空间:
PS> Get-ChildItem *.cs | Select-StringAligned -pattern override
---
FileWithQuiteALengthyName.cs : 34: protected override void Foo()
ShortName.cs : 46: protected override void Bar()
MediumNameFileHere.cs :123: protected override void Baz()
---
不幸的是,Select-String
没有这样做;实际上它给出了这个 - 你能在这里发现额外的空间吗?
PS> Get-ChildItem *.cs | Select-String -pattern override
---
FileWithQuiteALengthyName.cs:34: protected override void Foo()
ShortName.cs:46: protected override void Bar()
MediumNameFileHere.cs:123: protected override void Baz()
---
有没有办法强制Select-String
对齐其输出列?
编辑:
哎呀!我忘记了一个重要的部分:如果可能的话,我还想包含-Context
参数,这样就可以在比赛前后获得任意数量的线。
答案 0 :(得分:7)
使用对象:
Get-ChildItem *.cs | select-string -pattern override |
select Filename,LineNumber,Line | Format-Table -AutoSize
如果您决定要保留它,它也适合导出到csv。
添加该要求以便能够使用上下文会使事情变得相当复杂。
function ssalign {
begin {$display = @()}
process {
$_ -split "`n" |
foreach {
$display +=
$_ | New-PSObjectFromMatches -Pattern '^(.+)?:([\d]+):(.+)' -Property $null,File,Line,Text
}
}
end {
$display |
foreach {$_.line = ':{0,5}:' -f $_.line}
$display | ft -AutoSize -HideTableHeaders
}
}
Get-ChildItem *.cs | Select-StringAligned -pattern override | ssalign
在此处获取New-PSObjectFromMatches函数: http://gallery.technet.microsoft.com/scriptcenter/New-PSObjectFromMatches-87d8ce87
答案 1 :(得分:4)
这可能是:
Get-ChildItem *.cs | select-string -pattern override | ft filename, linenumber , line -AutoSize
或类似的东西:
Get-ChildItem *.cs | select-string -pattern override |
% { "{0,-20}{1,15}{2,70}" -f $_.filename, $_.linenumber , $_.line }
字符串格式-f
(“{0,-20} {1,15} {2,70}”)的第一部分中的值必须根据匹配的长度进行检查< / p>
答案 2 :(得分:1)
我给@mjolinor提供了复选标记,以获取他提供的优秀代码以及他付出的努力。尽管如此,一些读者可能会发现这种感兴趣的变化,特别是我为格式灵活性提供的NameWidth和NumberWidth参数:
filter Select-StringAligned
{
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string[]]$InputObject,
[Parameter(Mandatory=$true,Position=1)]
[string]$Pattern,
[int[]]$Context = @(0),
[int]$NameWidth = 35,
[int]$NumberWidth = 4)
select-string -Path $InputObject -Pattern $pattern -Context $Context |
% {
$entry = $_
if ($Context[0] -eq 0) { $offset = 0 }
else { $offset = @($entry.Context.PreContext).Count }
$linenum = $entry.LineNumber - $offset - 1
# For some reason need to special case the $list construction; otherwise acts like Context=(1,1)
if ($Context[0] + $Context[1] -eq 0) {
$list = $entry.Line
}
else {
$list = @($entry.Context.PreContext)
$list += $entry.Line
$list += $entry.Context.PostContext
}
$list | % {
if ($entry.FileName.length -lt $nameWidth) { $truncatedName = $entry.FileName }
else { $truncatedName=$entry.FileName.SubString(0,$NameWidth) }
"{0,-$NameWidth}:{1,$NumberWidth}: {2}" -f $truncatedName, $linenum++, $_
}
}
}
以下是一个示例用法。 -NameWidth参数允许字段宽度小于(截断)或大于(填充)文件名。
$pattern = "public"
Get-ChildItem *.cs |
Select-StringAligned -pattern $pattern -Context 0,2 -NameWidth 20 -NumberWidth 3