关于powershell:根据列值将一个CSV文件拆分为多个文件

Splitting a CSV file into multiple files based on column value

我是 PowerShell 新手,需要根据列值将 CSV 文件拆分为多个文件。

我的源文件是这样的

1
2
3
4
5
 ID   Name   TNumber
 123  Jo     123456
 123  Joe    789012
 124  Tom    896578
 124  Tom    403796

我阅读了这个帖子,它帮助我进行了分组,但我对如何根据 ID 列将其拆分为多个文件缺乏了解。这可能吗?


Group-Object 按任何列值,这里我们使用 ID

1
2
3
$groups = Import-CSV e:\\test.csv | Group-Object ID

$groups

输出

1
2
3
4
Count Name   Group
----- ----   -----
    2 123    {@{ID=123; Name=Jo; Tnumber=123456}, @{ID=123; Name=Joe;Tnumber=324233}}
    2 124    {@{ID=124; Name=Tom; Tnumber=5645645}, @{ID=124; Name=Tom; Tnumber=23423}}

最后把这个喂给for循环

1
$groups | ForEach-Object {$_.Group | Export-Csv"$($_.Name).csv" -NoTypeInformation}

如果您的文件有制表符分隔符,您可以这样做:

1
2
3
4
5
6
$CurDir="C:\\temp"

Import-Csv"$CurDir\\test.csv" -Delimiter"`t" | Group ID | %{
    $ID="{0}.csv" -f $_.Name
    $_.Group | export-csv"$CurDir\\$ID" -NoType
}

这是一个很老的问题。偶然发现了一个类似的场景,我必须根据文件中特定列的值从单个 csv 文件创建多个 CSV 文件。

我这样创建它是因为对我来说,我必须拆分的列名不是固定的


我来这里是为了寻找一个相当简单过程的快速答案,但大多数答案似乎对分组或行操作有点复杂。

下面的效果很好,我觉得更容易理解:

1
2
3
4
5
$users = Import-Csv -Path"C:\\example\\path\\users.csv" -Delimiter"`t"

foreach ($user in $users) {
    $user | Export-Csv -Path"C:\\example\\path\\$($user.ID).csv" -Append -NoTypeInformation
}

对于我的特殊情况,我们不希望输出文件中的特定列,所以我使用了 Select。在您的情况下,这看起来像:

1
2
3
4
5
$users = Import-Csv -Path"C:\\example\\path\\users.csv" -Delimiter"`t"

foreach ($user in $users) {
    $user | Select Name, TNumber | Export-Csv -Path"C:\\example\\path\\$($user.ID).csv"  -Delimiter"`t" -Append -NoTypeInformation
}

对不起,我误解了这个问题。重力,谢谢你的澄清。我认为其他一些答案可能有效,但如果他们不这样做,你可以试试这个。这可能是我会做的。请注意,我假设您有一个制表符分隔的文件,因此是 -delimiter"t". If it is comma separated, just remove the -delimiter"t"。

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
26
27
$users = import-csv users.csv -delimiter"`t"

# Loop through users.csv
foreach($user in $users)  
{
    # Put each field in a separate variable.
    $id = $user.ID;
    $name = $user.Name;
    $tnumber = $user.TNumber;

    # Write variables to host just to be sure the file is being read properly. This code can be commented out or removed after you are sure the file is being read.
    write-host $id;
    write-host $name;
    write-host $tnumber;
    write-host"";

    # If no file exists for a user with the ID contained in $id on this iteration, create it.
        if(!(Test-Path"$id.csv"))
        {
            out-file -filepath"$id.csv" -inputobject"ID`tName`tTNumber" -append;          
        }

    # Append record for the user with the ID contained in $id on this iteration to $id.csv
        out-file -filepath"$id.csv" -inputobject"$id`t$name`t$tnumber" -append;          
}

pause

也许我把它复杂化了,但以防万一我假设 Name 列包含一个中间名。像这样的东西:

1
2
3
4
5
6
7
ID     Name     TNumber
123    Jo       123456
123    Joe      789012
124    Tom      896578
124    Tom      403796
125    Jan W.   500300
125    Janny    700200

我的问题解决方法如下:

1
2
3
4
5
6
7
$csv = Get-Content .\\input.txt # source CSV file
$cap = $csv[0] -split '\\s+'    # caption of CSV
# replace spaces separating columns, group objects by ID
($csv[1..$csv.Length] -replace '(\\d+)\\s+(.*)\\s+(\\d+)', '$1,$2,$3' |
ConvertFrom-Csv -Delimiter ',' -Header $cap | Group-Object ID).ForEach{
  $_.Group | Export-Csv"$($_.Name).csv" # write result
}

您可以找到唯一的 ID 列表,然后使用 Where-Object 循环遍历它们,将每个 ID 过滤到单独的文件中。

1
2
3
4
5
$csv = Import-CSV $Path
$IDs = $csv.ID | Select-Object -Unique
foreach ($ID in $IDs) {
    $csv | Where-Object {$_.ID -eq $ID} | Export-CSV"C:\\example\\path\\$ID.csv"
}

使用 Where-Object 并不是特别有效,因为每次您都在搜索整个 csv。您可以使用具有内置 splitwhere() 方法(需要 PS4 )将 CSV 对象替换为新对象,该对象已删除先前过滤的值。这样,每次迭代的过滤价值就更少了。

1
2
3
4
5
6
$csv = Import-CSV $Path
$IDs = $csv.ID | Select-Object -Unique
foreach ($ID in $IDs) {
    $newfile,$csv = $csv.where({$_.ID -eq $ID},'Split')
    $newfile | Export-CSV"C:\\example\\path\\$ID.csv"
}

这假定您显示的源文件是逗号分隔的 csv 文件的格式化导入。否则使用 Import-CSV-delimiter 参数来设置正确的限制器。


这应该可以解决问题:

1
2
3
4
5
6
7
8
$fileContent = @(Get-Content -Path 'testfile.csv')

foreach( $line in $fileContent ) {
    $lineToken = ($line -replace '\\s+', ' ').Trim() -split ' '
    if( $lineToken[0] -match '^[0-9]+$' ) {
        $line | Out-File -FilePath ($lineToken[0] + '.csv') -Append
    }
}

您编写了 csv,所以我假设您的文件的纯文本版本如下所示:

ID,姓名,TNumber
123,乔,123456
123,乔,789012
124,汤姆,896578
124,汤姆,403796

我会这样做:

#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$users = import-csv users.csv

foreach($user in $users)  
{

    $id = $user.ID;
    $name = $user.Name;
    $tnumber = $user.TNumber;

    out-file -filepath id.csv -inputobject $id -append;
    out-file -filepath name.csv -inputobject $name -append;
    out-file -filepath tnumber.csv -inputobject $tnumber -append;

}

#

不是说如果你没有csv并且它是一个制表符分隔的文件,你可以在第一行添加以下属性:

-分隔符"`t"

希望这会有所帮助。