【问题标题】:Powershell Copy the CSV output and paste in second column of another CSVPowershell 复制 CSV 输出并粘贴到另一个 CSV 的第二列
【发布时间】:2019-08-12 03:41:25
【问题描述】:

这是关于 powershell 的。

我有一个名为 a 的 csv 文件,它在第一列中包含值 1 2 3 4 5,我还有另一个名为 b 的 csv 文件,它包含值 6 7 8 9 10。

b.csv 文件输出如下。

6  
7                                                                                                            
8                                                                                                                                                                                                                
9                                                                         
10

a.csv 文件输出如下。

1  
2                                                                                                            
3                                                                                                                                                                                                                
4                                                                         
5

想要的结果应该如下

1  6  
2  7                                                                                                          
3  8                                                                                                                                                                                                              
4  9                                                                       
5  10

我们正在将 b.csv 文件的输出复制到 column2 中的 a.csv 文件。

【问题讨论】:

  • 请添加示例输入数据以及您希望输出的排列方式。
  • @Lee_Dailey,感谢您回复并完成编辑。
  • 整洁!现在......这些文件中是否有标题?

标签: powershell


【解决方案1】:

这是完成这项工作的一种方法。 [grin] 它假定两个集合具有相同数量的项目。如果没有,就会出现错误。

# fake reading in a CSV file
#    in real life, use Import-CSV
$A_In = @'
1
2
3
4
5
'@ | ConvertFrom-Csv -Header 'A_Col'

# fake reading in another CSV file
$B_In = @'
6
7
8
9
10
'@ | ConvertFrom-Csv -Header 'B_Col'

# the ".GetUpperBound(0)" stuff gets the upper bound of the zero axis of the collection
#    that is the same as ".Count - 1"
$Results = foreach ($Index in 0..$A_In.GetUpperBound(0))
    {
    [PSCustomObject]@{
        A_Col = $A_In[$Index].A_Col
        B_Col = $B_In[$Index].B_Col
        }
    }

$Results

输出...

A_Col B_Col
----- -----
1     6    
2     7    
3     8    
4     9    
5     10   

此时,$Results 集合可以通过Export-CSV 优雅地发送到 CSV。

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2021-07-29
    • 2020-02-16
    • 1970-01-01
    相关资源
    最近更新 更多