Split a CSV file into multiple files and and give a unique name based on a Appname field. Fetch only account column in the newly created Appname-file

huangapple go评论64阅读模式
英文:

Split a CSV file into multiple files and and give a unique name based on a Appname field. Fetch only account column in the newly created Appname-file

问题

我有一个PowerShell查询,我需要根据应用程序名称拆分单个CSV文件为不同的文件,并仅保留帐户信息,忽略其余的列。请帮助我从PowerShell脚本中解决这个问题。

$InputFilePath = "E:\Test_csv\test.csv"
$SplitByColumnName = "AppName" #根据需要拆分的列名

$data = Import-Csv $InputFilePath | Select-Object -ExpandProperty $SplitByColumnName -Unique

ForEach ($i in $data)
{  
  $FinalFileNamePath = "E:\Test_csv\" + $i + ".CSV" #这是拆分文件存放的位置

  Import-Csv $InputFilePath | Where-Object {$_.$SplitByColumnName -eq $i } | Select-Object "Account" | Export-Csv $FinalFileNamePath -NoTypeInformation  
}

我尝试了上面的代码,但如何仅获取帐户名称而不是所有列?

英文:

I have a query in PowerShell, I need to split a single CSV file into different files based on the appname and with only account information and ignore the rest of the columns. Please help me here to solve it from a PowerShell script.

Split a CSV file into multiple files and and give a unique name based on a Appname field. Fetch only account column in the newly created Appname-file

Split a CSV file into multiple files and and give a unique name based on a Appname field. Fetch only account column in the newly created Appname-file

$InputFilePath = "E:\Test_csv\test.csv"
$SplitByColumnName = "AppName" #Enter ColumnName here on basis of which you want to split.

$data = Import-Csv $InputFilePath | Select -ExpandProperty $SplitByColumnName -Unique

$a = $data | select 

ForEach ($i in $a)
{  
  $FinalFileNamePath = "E:\Test_csv\" + $i + ".CSV" #This is where you would keep the splitted files.

  Import-Csv $InputFilePath | where {$_.$SplitByColumnName -eq $i } | Export-Csv $FinalFileNamePath -NoTypeInformation  
}

I tried the above code but how to get only the account name instead of all the columns?

答案1

得分: 0

$Csv = @'
name, account,  Appname, count, users, List
name1, account1,     123,     1,     2, abc
name1, account2,     123,     2,     3, z
name1, account3,     123,     3,     6, xc
name1, account4,     123,     5,     7, df
name1, account5,     123,     5,     8, rg
name1, account6,     123,     7,     0, sfg
name2, account1,     456,     1,     1, dfg
name3, account1,     789,     3,     7, rt
name3, account2,     789,     7,     1, ert
'@

$SplitByColumnName = "Appname" # Enter ColumnName here on basis of which you want to split.

$data = ConvertFrom-Csv $Csv
$AppNames = $Data | Select-Object -ExpandProperty $SplitByColumnName -Unique

ForEach ($Appname in $Appnames) {
  $FinalFileNamePath = "E:\Test_csv\" + $Appname + ".CSV" #This is where you would keep the splitted files.
  Import-Csv $InputFilePath |
    Where-Object Appname -eq $Appname |
    Select-Object Account |
    Export-Csv $FinalFileNamePath -NoTypeInformation  
}

Avoid wrapping cmdlet pipelines
Note that wrapping cmdlets (as Import-Csv and Export-Csv) in a loop is expensive as it (re)opens and closes the concerned files each cycle.
To avoid this in your case, you might use the steppable pipeline and create multiple output streams to fill the concerned exported CSV files.

$Pipeline = @{}
ConvertFrom-Csv $Csv | # Import-Csv $InputFilePath |
ForEach-Object -Process {
    if (!$Pipeline.Contains($_.Appname)) {
        $Pipeline[$_.Appname] = {
            Select-Object Account | Export-CSV -NoType -Path "E:\Test_csv$($_.Appname).CSV"  
        }.GetSteppablePipeline()
        $Pipeline[$_.Appname].Begin($True)
    }
    $Pipeline[$_.Appname].Process($_)
} -End {
    foreach ($Key in $Pipeline.Keys) { $Pipeline[$Key].End() }
}

For background and details, see: Mastering the (steppable) pipeline

英文:
$Csv = @'
name,  account,  Appname, count, users, List
name1, account1,     123,     1,     2, abc
name1, account2,     123,     2,     3, z
name1, account3,     123,     3,     6, xc
name1, account4,     123,     5,     7, df
name1, account5,     123,     5,     8, rg
name1, account6,     123,     7,     0, sfg
name2, account1,     456,     1,     1, dfg
name3, account1,     789,     3,     7, rt
name3, account2,     789,     7,     1, ert
'@

For what you tried:
You will need to filter (Where-Object) upon the Appname and select (Select-Object) upon the account name. If you use appropriate variable names (rather than $a and $i), things might get clearer:
<sup>(Personally, I value variable names more than comments)</sup>

$SplitByColumnName = &quot;AppName&quot; # Enter ColumnName here on basis of which you want to split.

$data = ConvertFrom-Csv $Csv # = Import-Csv $InputFilePath
$AppNames = $Data | Select-Object -ExpandProperty $SplitByColumnName -Unique

ForEach ($Appname in $Appnames) {
  $FinalFileNamePath = &quot;E:\Test_csv\&quot; + $Appname + &quot;.CSV&quot; #This is where you would keep the splitted files.
  Import-Csv $InputFilePath |
    Where-Object Appname -eq $Appname |
    Select-Object Account |
    Export-Csv $FinalFileNamePath -NoTypeInformation  
}

Avoid wrapping cmdlet pipelines
Note that wrapping cmdlets (as Import-Csv and Export-Csv) in a loop is expensive as it (re)opens and closes the concerns files each cycle.
To avoid this in your case, you might use the steppable pipeline and create multiple output streams to fill the concerned exported csv files.

$Pipeline = @{}
ConvertFrom-Csv $Csv | # Import-Csv $InputFilePath |
ForEach-Object -Process {
    if (!$Pipeline.Contains($_.Appname)) {
        $Pipeline[$_.Appname] = {
            Select-Object Account | Export-CSV -NoType -Path &quot;E:\Test_csv$($_.Appname).CSV&quot;  
        }.GetSteppablePipeline()
        $Pipeline[$_.Appname].Begin($True)
    }
    $Pipeline[$_.Appname].Process($_)
} -End {
    foreach ($Key in $Pipeline.Keys) { $Pipeline[$Key].End() }
}

For background and details, see: Mastering the (steppable) pipeline

huangapple
  • 本文由 发表于 2023年7月31日 22:44:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定