簡體   English   中英

如何在 Powershell 中使用 select-string 進行多個管道

[英]How to do multiple piping with select-string in Powershell

我用谷歌搜索但沒有找到我要找的東西。

我有一個包含國家和人員列表的大文件。 我知道如何在 Linux 中進行多個管道,但同樣的方法在 Powershell 中對我不起作用。

這就是我尋找的,一無所獲:

Select-String .\file -pattern 'country:[ ]{8}IR' -context 5 | Select-String -pattern 'names'

但是,如果我將此命令分成如下所示的工作(我想避免創建要搜索的文件):

Select-String .\file -pattern 'country:[ ]{8}IR' -context 5 > country
Select-String .\file -patern 'names'

*更新 1

第一個 grep 之后的示例數據是:

  file:1407215:names:        Hadi
  file:1407216:company:        sample
  file:1407217:city:          Tehran
  file:1407218:district:          8
  file:1407219:country:        IR
  file:1407220:admin:        Mahmoud
  file:1407221:tech:         Hamed
  file:1407222:seller:        sell@company
  file:1407223:status:         Active
  file:1407224:id:         12456

Select-String不返回[string] (或字符串數​​組),而是返回[MatchInfo]類型的對象。 MatchInfo 的輸出可能看起來像多行文本,但在屬性 .Context.PreContext 、 .Context.PreContext.Line中被拆分.Context.PostContext 因此,您不能直接使用此對象再次將其通過管道傳輸到Select-String中。

但是,您可以將輸出轉換為[String] ,在新行處-split它並在此數組上使用Select-String

$MatchInfo = Select-String $file -pattern 'country:[ ]{8}IR' -context 5
[string]$MatchInfo -split [environment]::NewLine | Select-String -pattern 'names'

從 PowerShell 的角度來看,您大部分時間都在處理對象,掌握處理它們的竅門可能是一個好主意,因此這個答案可以向您展示一種將文件解析為對象數組的替代方法,這些對象數組可以是易於操作、過濾、排序和導出為結構化數據(例如 Csv)

假設test.txt看起來像這樣:

names:        Hadi
company:        sample
city:          Tehran
district:          8
country:        IR
admin:        Mahmoud
tech:         Hamed
seller:        sell@company
status:         Active
id:         12456

names:        John
company:        sample
city:          Tehran
district:          8
country:        IR
admin:        Doe
tech:         Hamed
seller:        sell@company
status:         Disabled
id:         12456

對於這種特殊情況,我們可以使用帶有-Regex參數的switch來讀取文件,並使用-File開關讓條件子句開始捕獲並將捕獲數據作為對象輸出

$parsed = switch -Regex -File .\test.txt {
    # line starts with "names", signal to start capturing
    '^names' {
        $startcapture = $true
        $out = [ordered]@{}
    }
    # boolean is set to `$true`, capture this line and add it to the ordered dictionary
    { $startcapture } {
        $key, $value = $_.Split(':').Trim()
        $out[$key] = $value
    }
    # line starts with "id", signal to output the object, and restart the capture boolean
    '^id' {
        $startcapture = $false
        [pscustomobject] $out
    }
}

使用上述開關解析test.txt文件后, $parsed將如下所示:

names company city   district country admin   tech  seller       status   id
----- ------- ----   -------- ------- -----   ----  ------       ------   --
Hadi  sample  Tehran 8        IR      Mahmoud Hamed sell@company Active   12456
John  sample  Tehran 8        IR      Doe     Hamed sell@company Disabled 12456

現在$parsed可以使用Export-Csv輕松導出到結構化數據,並使用Import-Csv作為對象導入回來:

$parsed | Export-Csv parseddata.csv -NoTypeInformation
$csv = Import-Csv parseddata.csv

它也可以很容易地被過濾 cmdlet 過濾,例如Where-Object

# this array of objects where the `country` property value is equal to "IR"
# AND this array of objects where the `names` property value is equal to "Hadi"
$parsed | Where-Object { $_.country -eq 'IR' -and $_.names -eq 'Hadi' }

結果是:

names    : Hadi
company  : sample
city     : Tehran
district : 8
country  : IR
admin    : Mahmoud
tech     : Hamed
seller   : sell@company
status   : Active
id       : 12456

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM