如何以表格形式排列不同数组的值?
How to arrange values of different arrays in a tabular form?
我有发件人、收件人、主题三个数组;我需要将它们以表格形式排列如下。
Sender Recipient Subject
xxxx@xxx.com xxx@xxx.com xxx
这是我的代码
dir|ForEach-Object {
$Name=$_.Name
foreach ($N in $Name) { Import-Csv $N|`
Foreach {
$Sender+=$_.SenderAddress
$Recipient+=$_.RecipientAddress
$Subject+=$_.Subject
}
}
}
任何人都可以调查一下吗?
此致,
基兰
如果您有多个完美对齐的数组,您可以使用 for
循环轻松地将它们压缩在一起:
$Sender = @('jack@company.example','jill@company.example')
$Recipient = @('alice@company.example','bob@company.example')
$Subject = @('Overdue invoices','Receipts')
$mailObjects = for($i = 0; $i -lt $Sender.Count; $i++){
New-Object psobject -Property @{
Sender = $Sender[$i]
Recipient = $Recipient[$i]
Subject = $Subject[$i]
}
}
现在您可以使用 Format-Table
:
PS C:\> $mailObjects |Format-Table
Sender Recipient Subject
------ --------- -------
jack@company.example alice@company.example Overdue invoices
jill@company.example bob@company.example Receipts
如果我对您的代码的解释是正确的,那么您指的是 csv 文件的列而不是数组。导入后所需属性的 select 应该就足够了,新的更短的 属性 名称可以通过 [pscustomobject]
.
实现
假设有:
> Get-Content first.csv
"othercol","SenderAddress","RecipientAddress","Subject"
"","jack@company.example","alice@company.example","Overdue invoices"
"","jill@company.example","bob@company.example","Receipts"
和:
>Get-Content second.csv:
"SenderAddress","RecipientAddress","Subject","othercol"
"john@company.example","anna@company.example","Overdue deliveries","xyz"
"jim@company.example","willb@company.example","Orders","abc"
这一班:
foreach ($File in (gci *.csv)){Import-Csv $File|Select SenderAddress,Recipientaddress,Subject}
将输出:
SenderAddress RecipientAddress Subject
------------- ---------------- -------
jack@company.example alice@company.example Overdue invoices
jill@company.example bob@company.example Receipts
john@company.example anna@company.example Overdue deliveries
jim@company.example willb@company.example Orders
这个更详细的版本:
ForEach ($File in (Get-ChildItem *.csv)){
Import-Csv $File | Select-Object SenderAddress,Recipientaddress,Subject |
ForEach-Object{[pscustomobject] @{Sender = $_.SenderAddress
Recipient = $_.Recipientaddress
Subject = $_.Subject}
}
}
产生这个输出:
Sender Recipient Subject
------ --------- -------
jack@company.example alice@company.example Overdue invoices
jill@company.example bob@company.example Receipts
john@company.example anna@company.example Overdue deliveries
jim@company.example willb@company.example Orders
我有发件人、收件人、主题三个数组;我需要将它们以表格形式排列如下。
Sender Recipient Subject
xxxx@xxx.com xxx@xxx.com xxx
这是我的代码
dir|ForEach-Object {
$Name=$_.Name
foreach ($N in $Name) { Import-Csv $N|`
Foreach {
$Sender+=$_.SenderAddress
$Recipient+=$_.RecipientAddress
$Subject+=$_.Subject
}
}
}
任何人都可以调查一下吗?
此致, 基兰
如果您有多个完美对齐的数组,您可以使用 for
循环轻松地将它们压缩在一起:
$Sender = @('jack@company.example','jill@company.example')
$Recipient = @('alice@company.example','bob@company.example')
$Subject = @('Overdue invoices','Receipts')
$mailObjects = for($i = 0; $i -lt $Sender.Count; $i++){
New-Object psobject -Property @{
Sender = $Sender[$i]
Recipient = $Recipient[$i]
Subject = $Subject[$i]
}
}
现在您可以使用 Format-Table
:
PS C:\> $mailObjects |Format-Table
Sender Recipient Subject
------ --------- -------
jack@company.example alice@company.example Overdue invoices
jill@company.example bob@company.example Receipts
如果我对您的代码的解释是正确的,那么您指的是 csv 文件的列而不是数组。导入后所需属性的 select 应该就足够了,新的更短的 属性 名称可以通过 [pscustomobject]
.
假设有:
> Get-Content first.csv
"othercol","SenderAddress","RecipientAddress","Subject"
"","jack@company.example","alice@company.example","Overdue invoices"
"","jill@company.example","bob@company.example","Receipts"
和:
>Get-Content second.csv:
"SenderAddress","RecipientAddress","Subject","othercol"
"john@company.example","anna@company.example","Overdue deliveries","xyz"
"jim@company.example","willb@company.example","Orders","abc"
这一班:
foreach ($File in (gci *.csv)){Import-Csv $File|Select SenderAddress,Recipientaddress,Subject}
将输出:
SenderAddress RecipientAddress Subject
------------- ---------------- -------
jack@company.example alice@company.example Overdue invoices
jill@company.example bob@company.example Receipts
john@company.example anna@company.example Overdue deliveries
jim@company.example willb@company.example Orders
这个更详细的版本:
ForEach ($File in (Get-ChildItem *.csv)){
Import-Csv $File | Select-Object SenderAddress,Recipientaddress,Subject |
ForEach-Object{[pscustomobject] @{Sender = $_.SenderAddress
Recipient = $_.Recipientaddress
Subject = $_.Subject}
}
}
产生这个输出:
Sender Recipient Subject
------ --------- -------
jack@company.example alice@company.example Overdue invoices
jill@company.example bob@company.example Receipts
john@company.example anna@company.example Overdue deliveries
jim@company.example willb@company.example Orders