输出选择字符串上下文所有行值

时间:2018-01-25 17:53:45

标签: powershell powershell-v5.0 powershell-ise

我正在使用Select-String来查找一堆.dco中的模式 并使用这一行我得到:

PS C:\> gci $folder -filter *.dco -Recurse | Select-String -Pattern $pattern 

C:\path\path\23_DCO\2017\12 December\DCO_CYC15_E_15122017.DCO:373:MTR150000010010          0  0RA              409347    0100000125000001250000140000NN  NNE 000

模式匹配,我得到那条线。我需要的实际数据行在它下面,所以我添加参数-Context

PS C:\> gci $folder -filter *.dco -Recurse | Select-String -Pattern $pattern -Context 1

  C:\path\path\23_DCO\2017\12 December\DCO_CYC15_E_15122017.DCO:372:CUS150000010010001288502                                     
> C:\path\path\23_DCO\2017\12 December\DCO_CYC15_E_15122017.DCO:373:MTR150000010010          0  0RA              409347    0100000125000001250000140000NN  NNE 000                            
  C:\path\path\23_DCO\2017\12 December\DCO_CYC15_E_15122017.DCO:374:RDG15000001KH01R000000000K00000100000000271000012152017000000N000000000000UMETERSENS000000000K 10000000000000000267  

这是我关注的第3行,但是所有3行信息都很有用,所以我希望将其导出。

这行代码向我提供了我想要输出的数据:

$Found | Select * -ExcludeProperty RowError, RowState, HasErrors, Table, ItemArray, IgnoreCase, Context | Export-Excel

LineNumber : 373
Line       : MTR150000010010          0  0RA              409347    0100000125000001250000140000NN  NNE 000                            
Filename   : DCO_CYC15_E_15112017_2.DCO
Path       : P:\path\path\23_DCO\2017\11 November\DCO_CYC15_E_15112017_2.DCO
Pattern    : 409347
Matches    : {0}

然而,这只是" Line中的匹配行:"属性。 如何在-Context中包含另外两行?

如果无法做到这一点,我对这一行感到满意,这是来自-Context 1输出的第3行:

 RDG15000001KH01R000000000K00000100000000271000012152017000000N000000000000UMETERSENS000000000K 10000000000000000267

如果我无法获得所有3行,我如何将其切换为导出中包含的行数据?

注意: 这都是在一个循环中完成的。在这种情况下,只有一个匹配,但可能有几个。

$Found = @()
Foreach ($folder in $folders)
{
    $Found += gci $folder -filter *.dco -Recurse | Select-String -Pattern $pattern -Context 1 
    if ($Found) {
        $Found | Select * -ExcludeProperty RowError, RowState, HasErrors, Table, ItemArray, IgnoreCase, Context | Export-Excel -Path $savepathr -WorkSheetname "$pattern" -AutoSize -BoldTopRow -FreezeTopRow
        $row.Status = "Found in File"
    }
}

谢谢,希望你明白我在问什么。

1 个答案:

答案 0 :(得分:1)

使用计算属性从Context属性中获取第3行:

$Found | Select *,@{Name='PostContext';Expression={$_.Context.PostContext}} -ExcludeProperty RowError, RowState, HasErrors, Table, ItemArray, IgnoreCase, Context | Export-Excel -Path $savepathr -WorkSheetname "$pattern" -AutoSize -BoldTopRow -FreezeTopRow

由于$Found += ...语句,然后导出到目前为止捕获的所有已找到实例,您当前的循环将导致重复。

$Found = foreach ($folder in $folders) {
    Get-ChildItem $folder -Filter *.dco -Recurse | Select-String -Pattern $pattern -Context 1 
}
if ($Found) {
    $Found | Select *,@{Name='PostContext';Expression={$_.Context.PostContext}} -ExcludeProperty RowError, RowState, HasErrors, Table, ItemArray, IgnoreCase, Context | Export-Excel -Path $savepathr -WorkSheetname "$pattern" -AutoSize -BoldTopRow -FreezeTopRow
    $row.Status = "Found in File"
}