【问题标题】:Split CSV file by column number using Powershell使用 Powershell 按列号拆分 CSV 文件
【发布时间】:2018-12-31 14:57:23
【问题描述】:

我有一个包含大约 2500 列的 CSV 文件,没有按管道分割的标题 |分隔符。

file.csv 看起来像这样:

x,y,z,x1,x2,x3,x4,x5,x6,x7,x8,x9,...(about 2500 more)...,x2500
0,0,0,a1,a2,a3,a4,a5,a6,a7,a8,a9,...(about 2500 more)...,s2500
1,1,1,b1,b2,b3,b4,b5,b6,b7,b8,b9,...(about 2500 more)...,b2500
….

我想根据列号将此文件拆分为多个文件。 使用 Bash,我使用了 cut -d "|" -f1,2-901 并选择了我想保存在新文件中的列。

输出:

file1.csv

Key1,x2,x3,x4,x5,x6,x7,x8,x9,...(about 900 more)...,x900  
Key2,a2,a3,a4,a5,a6,a7,a8,a9,...(about 900 more)...,a900  
Key3,b2,b3,b4,b5,b6,b7,b8,b9,...(about 900 more)...,b900 <BL>
… 

file2.csv

Key1,x901,x902,x903,x904,...(about 900 more)...,x1800  
Key2,a901,a902,a904,a904,...(about 900 more)...,a1800  
Key3,b901,b902,b903,b904,...(about 900 more)...,b1800  
…

如何在 Powershell 中进行操作?

任何帮助将不胜感激。

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    使用 Select-Object 只获取想要的列。

    为标题/属性构建一个数组

    此脚本创建一个只有 25 列的 csv 用于演示

    ## Q:\CsvData\2018\12\31\SO_53988782.ps1
    $file = '.\Data.csv'
    
    #create sample csv with headers x1..x25
    (1..25|ForEach-Object{"x{0}" -f $_}) -join ',' | set-content $file
    (1..25|ForEach-Object{$_}) -join ',' | add-content $file
    
    Get-Content $file
    
    $Range1 = 1..9  | ForEach-Object{"x{0}" -f $_}
    $Range2 = 10..19| ForEach-Object{"x{0}" -f $_}
    
    $CsvData = Import-csv $file
    
    $CsvData | Select-Object $Range1 | Format-Table -auto
    
    $CsvData | Select-Object $Range2 | Format-Table -auto
    

    > Get-Content $file
    x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25
    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25
    

    > $CsvData | Select-Object $Range1 | Format-Table -auto
    
    x1 x2 x3 x4 x5 x6 x7 x8 x9
    -- -- -- -- -- -- -- -- --
    1  2  3  4  5  6  7  8  9
    

    > $CsvData | Select-Object $Range2 | Format-Table -auto
    
    x10 x11 x12 x13 x14 x15 x16 x17 x18 x19
    --- --- --- --- --- --- --- --- --- ---
    10  11  12  13  14  15  16  17  18  19
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多