PowerShell 自定义 Table 并将行转换为列
PowerShell Custom Table and convert row to column
我有一个数组中的文件
$txt = @(P123456N123, P123456N122, P123456N223, P12asd6N122, P12asd6N124,
P12asd6N201, P1235d6N124, P1235d6N123, P1235d6N122)
我需要获取每个文件的最后三位数字,已经完成了。我正在努力的是得到一个如下所示的 table:
File Name
Numbers
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
但这给了我一个非常格式化的结果并且它不是 portable 到下面列出的格式的 csv 中。任何帮助将不胜感激。
您可以使用 计算表达式 和 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 位数字结尾。
尝试以下结合 Group-Object
with Select-Object
and calculated properties:
@(
'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 ', ' } }
To-display 输出:
File Name Numbers
--------- -------
P123456N 123, 122, 223
P1235d6N 124, 123, 122
P12asd6N 122, 124, 201
我有一个数组中的文件
$txt = @(P123456N123, P123456N122, P123456N223, P12asd6N122, P12asd6N124,
P12asd6N201, P1235d6N124, P1235d6N123, P1235d6N122)
我需要获取每个文件的最后三位数字,已经完成了。我正在努力的是得到一个如下所示的 table:
File Name | Numbers |
---|---|
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
但这给了我一个非常格式化的结果并且它不是 portable 到下面列出的格式的 csv 中。任何帮助将不胜感激。
您可以使用 计算表达式 和 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 位数字结尾。
尝试以下结合 Group-Object
with Select-Object
and calculated properties:
@(
'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 ', ' } }
To-display 输出:
File Name Numbers
--------- -------
P123456N 123, 122, 223
P1235d6N 124, 123, 122
P12asd6N 122, 124, 201