如何以表格形式排列不同数组的值?

时间:2017-09-10 11:33:29

标签: powershell dataformat

我有三个数组,如Sender,Recipient和Subject;我需要以表格形式安排它们,如下所示。

  this.route.paramMap
      .switchMap((params: ParamMap) => this.userService.getUser(params.get('email')));
      .subscribe(function(x) { console.log(x); }); 

这是我的代码

Sender              Recipient             Subject
xxxx@xxx.com        xxx@xxx.com           xxx

任何人都可以看一下吗?

此致 基兰

2 个答案:

答案 0 :(得分:2)

如果你有多个完美对齐的数组,你可以使用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

答案 1 :(得分:0)

如果我正确地解释你的代码,你指的是csv文件的列而不是数组。导入后,选择所需的属性应该足够了,可以使用[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