【问题标题】:Split content from one .csv to multiple files based on content using powershell使用powershell根据内容将内容从一个.csv拆分为多个文件
【发布时间】:2021-06-08 06:54:50
【问题描述】:

我有一个包含两种类型行的 .csv 文件。第一个包含标题信息。它总是以AB 开头。第二种类型包含内容。这个总是以CD 开头。 每个标题行之后可以有多个内容行(总是至少一个)。它们一直在一起,直到下一个标题行(再次以 AB 开头)。

例子:

header1; header2; header3; header4; header5; header6; header7
AB; 12345; AB123456789; 10.03.2021; GT; BC987654321; EUR
CD; 456789; 22.24; Text; SW;
AB; 12345; AB123456789; 10.03.2021; GT; BC987654322; EUR
CD; 354345; 85.45; Text; SW;
CD; 123556; 94.63; Text; SW;
CD; 354564; 12.34; Text; SW;
CD; 135344; 32.23; Text; SW;
AB; 12345; AB123456789; 10.03.2021; GT; BC987654323; EUR
CD; 354564; 12.34; Text; SW;
CD; 852143; 34.97; Text; SW;

如何使用 PowerShell 将此文件拆分为多个 .csv 文件 - 每个标题行 (AB) 一个。我想要的结果是

BC987654321.csv

header1; header2; header3; header4; header5; header6; header7
AB; 12345; AB123456789; 10.03.2021; GT; BC987654321; EUR
CD; 456789; 22.24; Text; SW;

BC987654322.csv

header1; header2; header3; header4; header5; header6; header7
AB; 12345; AB123456789; 10.03.2021; GT; BC987654322; EUR
CD; 354345; 85.45; Text; SW;
CD; 123556; 94.63; Text; SW;
CD; 354564; 12.34; Text; SW;
CD; 135344; 32.23; Text; SW;

BC987654323.csv

header1; header2; header3; header4; header5; header6; header7
AB; 12345; AB123456789; 10.03.2021; GT; BC987654323; EUR
CD; 354564; 12.34; Text; SW;
CD; 852143; 34.97; Text; SW;

我根本不习惯 PowerShell - 所以我会很感激新手友好的解决方案。

非常感谢您。

【问题讨论】:

  • 你能告诉我们你尝试了什么吗?
  • 这是您的 CSV 文件的真实示例吗?数据在哪里,为什么我们只看到标题?你想要的输出是什么?
  • @Theo 这些不是标题,而是随机选择的单元格名称。我刚刚编辑了这个问题,所以这个例子现在看起来更像实际文件了。
  • @AbrahamZinala 我目前正在阅读 PowerShell 的一般工作原理,正如我在问题中提到的那样,我使用它的经验几乎为零。我至少可以在文件中添加一个标题(提供的文件中没有),所以从现在开始使用这些列可能会更容易。

标签: powershell csv search import-csv export-csv


【解决方案1】:

如果我理解正确,您希望在“header1”等于“AB”的每一行上拆分 csv,然后使用“header6”下该行中的内容作为输出文件名。

$path = 'D:\Test'
$fileIn = Join-Path -Path $path -ChildPath 'input.csv'
$fileOut = $null   # will get a value in the loop
$splitValue = 'AB' # the header1 value that decides to start a new file
$csv = Import-Csv -Path $fileIn -Delimiter ';'
# get an array of the column headers
$allHeaders = $csv[0].PsObject.Properties.Name
foreach ($item in $csv) {
    if ($item.header1 -eq $splitValue) { 
        # start a new file
        $fileOut = Join-Path -Path $path -ChildPath ('{0}.csv' -f $item.header6)
        # create the new csv file with the first row of data already in it
        $item | Select-Object $allHeaders | Export-Csv -Path $fileOut -Delimiter ';' -NoTypeInformation
    }
    else {
        # rows with header1 not 'AB' are added to that file
        if ([string]::IsNullOrEmpty($fileOut)) {
            Write-Warning "Could not find a starting row (header1 = '$splitValue') for the file"
        }
        else {
            $item | Select-Object $allHeaders | Export-Csv -Path $fileOut -Delimiter ';' -Append
        }
    }
}

当然,更改路径以匹配您的环境。

输出:

BC987654321.csv

"header1";"header2";"header3";"header4";"header5";"header6";"header7"
"AB";"12345";"AB123456789";"10.03.2021";"GT";"BC987654321";"EUR"
"CD";"456789";"22.24";"Text";"SW";"";

BC987654322.csv

"header1";"header2";"header3";"header4";"header5";"header6";"header7"
"AB";"12345";"AB123456789";"10.03.2021";"GT";"BC987654322";"EUR"
"CD";"354345";"85.45";"Text";"SW";"";
"CD";"123556";"94.63";"Text";"SW";"";
"CD";"354564";"12.34";"Text";"SW";"";
"CD";"135344";"32.23";"Text";"SW";"";

BC987654323.csv

"header1";"header2";"header3";"header4";"header5";"header6";"header7"
"AB";"12345";"AB123456789";"10.03.2021";"GT";"BC987654323";"EUR"
"CD";"354564";"12.34";"Text";"SW";"";
"CD";"852143";"34.97";"Text";"SW";;

【讨论】:

  • 我在$item | Export-Csv -Path $fileOut -Delimiter ';' -Append 上收到一条错误消息,提示 Path 为 NULL 或空。
  • @Nerevar.de 不,这意味着没有起始行 header1 包含值 AB。我只有你的例子,在现实生活中,你需要将AB 更改为文件中第一行拆分的真正含义。我已经更新了我的答案,所以现在它会显示一条消息并添加一个新变量$splitValue,这样您就可以简单地将其设置为真实文件中的内容。
  • 是的,我得到了那个。在您更新之前,我也更改了它。但即使您使用新变量的解决方案也具有相同的效果。使用您编辑的代码,它现在显示警告而不是错误消息。甚至尝试将文件的内容更改为 AB 以从另一侧进行测试。
  • @Nerevar.de 然后请编辑您的问题并按原样显示文件的前 3 或 4 行。使用示例有效。
  • 我的错。我一直缺少更改“header1”,这在真实文件中也明​​显不同。我还创建了关于合并这些确切行的最后一个问题。 stackoverflow.com/questions/66566801 如果您也检查一下,我会很高兴。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2021-06-08
  • 2013-01-09
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 2016-06-04
相关资源
最近更新 更多