需要使用 PowerShell 更新 CSV 中的行
Need to update rows in CSV using PowerShell
我有一个 CSV 格式的文档列表 属性,其中包含每年不同的元数据值,我需要逐行更新它。
Example :
FileName Year Department Owner
----------------------------------------------------------------------------------
Invoice_SIMS 2019 Sales Alan
Invoice_SIMS 2018 Sales Alan
Invoice_SIMS 2016 Sales APAC Elizabeth
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2018 Sales APAC Tan
Invoice_PAS 2017 Sales Singapore Lim'
我需要更新以下信息,这些信息是我从 CSV 中获得的,符合这些文件的最高排名 'Year'。
例如。 Invoice_SIMS 年份的最高值为 2019 年最高,它将更新最新的元数据到上一年的版本。
我尝试在 PowerShell 中实现的示例数据
FileName Year Department Owner
------------------------------------------------------------------------------------
Invoice_SIMS 2019 Sales Alan
Invoice_SIMS 2018 Sales Alan
Invoice_SIMS 2016 Sales Alan
Invoice_SIMS 2016 Sales Alan
Invoice_SIMS 2019 Sales Alan
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2020 Sales Europe Abraham
年将维护此文件名的任何 primary/unique 元数据。
谢谢。
更新 - 以下是我的代码..但是我一直在尝试更新剩余的文档并导出到 excel。
$SpreedsheetPath =".\SalesReport.csv"
function Get-HighestYearMetadata{
param(
[Parameter(Mandatory = $true)] [string] $SpreedsheetPath
)
if ($SpreedsheetPath -ne $null)
{
Write-Output 'Analysing highest Year for each documents..'
#Generate the list of documents that have the highest Year
$FinalReport = Import-Csv $SpreedsheetPath
$FinalReportGrouped = $FinalReport | Group-Object {$_.FileName}
ForEach ($item in $FinalReportGrouped) {
$item.Group | Sort-Object -Descending -Property Year | Select-Object -First 1
}
#2. Get the metadata for the document and assign to the remaining document that have same filename
foreach ($row in $FinalReportGrouped)
{
$FinalReport | Where {$_.FileName -eq $row.FileName}
{
Write-Output 'Output Department : ' $row.Department
Write-Output 'Output Owner : ' $row.Owner
$_.Department = $row.Department
$_.Owner = $row.Owner
}
}
Write-Output 'Exporting CSV'
$FinalReport | Export-Csv -LiteralPath $DestinationPath -Encoding UTF8 -NoTypeInformation
}
else
{
Write-Output 'Error : File Path is empty'
}
}
您可以使用 Sort-Object
cmdlet 和一些代码来获得所需的结果。
# test data creation
$csv = @'
Invoice_SIMS 2019 Sales Alan
Invoice_SIMS 2018 Sales Alan
Invoice_SIMS 2016 Sales APAC Elizabeth
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2018 Sales APAC Tan
Invoice_PAS 2017 Sales Singapore Lim
'@ -split [System.Environment]::NewLine
$csv = $csv | ForEach-Object -Process {$_ -split ' {2,}' -join ','}
$csv = ConvertFrom-Csv -InputObject $csv -Header FileName, Year, Department, Owner
# get the name of a temporary file for storing changed test data
$tempcsvfilename = [System.IO.Path]::GetTempFileName()
# modify test data and save it in a temporary file
$curr_filename = ''
$csv | Sort-Object -Property FileName, Year -Descending |
ForEach-Object -Process {
if ( $_.FileName -ne $curr_filename ) {
$curr_filename, $dept, $owner = $_.FileName, $_.Department, $_.Owner
} else {
$_.Department, $_.Owner = $dept, $owner
}
$_
} | Export-Csv -LiteralPath $tempcsvfilename -Encoding UTF8 -NoTypeInformation
# view modified test data
Get-Content -LiteralPath $tempcsvfilename -Encoding UTF8
输出:
"FileName","Year","Department","Owner"
"Invoice_SIMS","2019","Sales","Alan"
"Invoice_SIMS","2018","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_PAS","2020","Sales Europe","Abraham"
"Invoice_PAS","2018","Sales Europe","Abraham"
"Invoice_PAS","2017","Sales Europe","Abraham"
如果我们假设 File.csv
包含您的逗号分隔格式的数据,您可以执行以下操作。
$data = Import-Csv File.csv
$dataToCopy = $data | Group-Object FileName | Foreach-Object {
$_.Group | Sort Year -Desc | Select -First 1
}
foreach ($row in $dataToCopy) {
$data | Where {$_.FileName -eq $row.FileName} | Foreach-Object {
$_.Department = $row.Department
$_.Owner = $row.Owner
}
}
$data | Export-Csv NewFile.csv -NoType
上述解决方案保留了您当前的数据排序。
我有一个 CSV 格式的文档列表 属性,其中包含每年不同的元数据值,我需要逐行更新它。
Example :
FileName Year Department Owner
----------------------------------------------------------------------------------
Invoice_SIMS 2019 Sales Alan
Invoice_SIMS 2018 Sales Alan
Invoice_SIMS 2016 Sales APAC Elizabeth
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2018 Sales APAC Tan
Invoice_PAS 2017 Sales Singapore Lim'
我需要更新以下信息,这些信息是我从 CSV 中获得的,符合这些文件的最高排名 'Year'。
例如。 Invoice_SIMS 年份的最高值为 2019 年最高,它将更新最新的元数据到上一年的版本。
我尝试在 PowerShell 中实现的示例数据
FileName Year Department Owner
------------------------------------------------------------------------------------
Invoice_SIMS 2019 Sales Alan
Invoice_SIMS 2018 Sales Alan
Invoice_SIMS 2016 Sales Alan
Invoice_SIMS 2016 Sales Alan
Invoice_SIMS 2019 Sales Alan
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2020 Sales Europe Abraham
年将维护此文件名的任何 primary/unique 元数据。
谢谢。
更新 - 以下是我的代码..但是我一直在尝试更新剩余的文档并导出到 excel。
$SpreedsheetPath =".\SalesReport.csv"
function Get-HighestYearMetadata{
param(
[Parameter(Mandatory = $true)] [string] $SpreedsheetPath
)
if ($SpreedsheetPath -ne $null)
{
Write-Output 'Analysing highest Year for each documents..'
#Generate the list of documents that have the highest Year
$FinalReport = Import-Csv $SpreedsheetPath
$FinalReportGrouped = $FinalReport | Group-Object {$_.FileName}
ForEach ($item in $FinalReportGrouped) {
$item.Group | Sort-Object -Descending -Property Year | Select-Object -First 1
}
#2. Get the metadata for the document and assign to the remaining document that have same filename
foreach ($row in $FinalReportGrouped)
{
$FinalReport | Where {$_.FileName -eq $row.FileName}
{
Write-Output 'Output Department : ' $row.Department
Write-Output 'Output Owner : ' $row.Owner
$_.Department = $row.Department
$_.Owner = $row.Owner
}
}
Write-Output 'Exporting CSV'
$FinalReport | Export-Csv -LiteralPath $DestinationPath -Encoding UTF8 -NoTypeInformation
}
else
{
Write-Output 'Error : File Path is empty'
}
}
您可以使用 Sort-Object
cmdlet 和一些代码来获得所需的结果。
# test data creation
$csv = @'
Invoice_SIMS 2019 Sales Alan
Invoice_SIMS 2018 Sales Alan
Invoice_SIMS 2016 Sales APAC Elizabeth
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2018 Sales APAC Tan
Invoice_PAS 2017 Sales Singapore Lim
'@ -split [System.Environment]::NewLine
$csv = $csv | ForEach-Object -Process {$_ -split ' {2,}' -join ','}
$csv = ConvertFrom-Csv -InputObject $csv -Header FileName, Year, Department, Owner
# get the name of a temporary file for storing changed test data
$tempcsvfilename = [System.IO.Path]::GetTempFileName()
# modify test data and save it in a temporary file
$curr_filename = ''
$csv | Sort-Object -Property FileName, Year -Descending |
ForEach-Object -Process {
if ( $_.FileName -ne $curr_filename ) {
$curr_filename, $dept, $owner = $_.FileName, $_.Department, $_.Owner
} else {
$_.Department, $_.Owner = $dept, $owner
}
$_
} | Export-Csv -LiteralPath $tempcsvfilename -Encoding UTF8 -NoTypeInformation
# view modified test data
Get-Content -LiteralPath $tempcsvfilename -Encoding UTF8
输出:
"FileName","Year","Department","Owner"
"Invoice_SIMS","2019","Sales","Alan"
"Invoice_SIMS","2018","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_PAS","2020","Sales Europe","Abraham"
"Invoice_PAS","2018","Sales Europe","Abraham"
"Invoice_PAS","2017","Sales Europe","Abraham"
如果我们假设 File.csv
包含您的逗号分隔格式的数据,您可以执行以下操作。
$data = Import-Csv File.csv
$dataToCopy = $data | Group-Object FileName | Foreach-Object {
$_.Group | Sort Year -Desc | Select -First 1
}
foreach ($row in $dataToCopy) {
$data | Where {$_.FileName -eq $row.FileName} | Foreach-Object {
$_.Department = $row.Department
$_.Owner = $row.Owner
}
}
$data | Export-Csv NewFile.csv -NoType
上述解决方案保留了您当前的数据排序。