我在陣列中有檔案
$txt = @(P123456N123, P123456N122, P123456N223, P12asd6N122, P12asd6N124,
P12asd6N201, P1235d6N124, P1235d6N123, P1235d6N122)
我需要得到每個檔案的最后三位數字,已經完成了。我正在努力獲得一張如下所示的表格:
| 檔案名 | 數字 |
|---|---|
| P123456N | 122、123、223 |
| P12asd6N | 122、124、201 |
| P1235d6N | 122、123、124 |
到目前為止,我所擁有的是:
$table = @()
foreach ($file in $txt){
$subNo = $file.basename.substring(($file.basename.length - 4),3)
$FileName = $file.name.substring(0, 8)
$table = [PSCustomObject] @{
FileName = $FileName
SubNo = $subNo
}
}
然后我可以這樣做$table | Group-Object FileName,但這給了我一個非常格式化的結果,并且它不能移植到下面列出的格式的 csv 中。任何幫助,將不勝感激。
uj5u.com熱心網友回復:
嘗試以下結合Group-Object和計算Select-Object的屬性:
@(
'P123456N123', 'P123456N122', 'P123456N223', 'P12asd6N122', 'P12asd6N124',
'P12asd6N201', 'P1235d6N124', 'P1235d6N123', 'P1235d6N122'
) |
Group-Object { $_.Substring(0, 8) } |
Select-Object @{ Name='File Name'; Expression='Name'},
@{ Name='Numbers'; Expression={ $_.Group.Substring(8) -join ', ' } }
顯示輸出:
File Name Numbers
--------- -------
P123456N 123, 122, 223
P1235d6N 124, 123, 122
P12asd6N 122, 124, 201
uj5u.com熱心網友回復:
您可以使用計算運算式對值進行Group-Object分組,然后遍歷分組物件以獲得所需的輸出:
$txt = @(
'P123456N123', 'P123456N122', 'P123456N223', 'P12asd6N122', 'P12asd6N124',
'P12asd6N201', 'P1235d6N124', 'P1235d6N123', 'P1235d6N122'
)
$txt | Group-Object { $_ -replace '\d{3}$' } | ForEach-Object {
[pscustomobject]@{
'File Name' = $_.Name
'Numbers' = $_.Group -replace '. (?=\d{3}$)' -join ', '
}
}
正則運算式意味著所有檔案都以 3 個數字結尾。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475906.html
標籤:电源外壳
