根据相同的列值合并csv的行

时间:2019-12-24 14:33:20

标签: powershell

Entity          Entry type                               Time       
------          ----------                               ----       
abab            Recording stopped                        10:48:01 AM
abab            Recording started                        10:49:14 AM
fghi            Recording stopped                        10:11:40 AM
fghi            Recording started                        10:12:13 AM
fghi            Recording stopped                        11:13:01 AM
fghi            Recording started                        9:14:13 AM 
tr              Recording stopped                        8:45:01 AM 
tr              Recording started                        11:14:18 AM

我有一个csv文件,并且我想要同一行中相同实体列值的数据。 例如:数据“ abab”将在一行中。 我正在尝试通过电源外壳实现这一点。 抱歉,如果怀疑太基础了。

1 个答案:

答案 0 :(得分:0)

如果这是您的CSV输入文件

"Entity","Entry type","Time"
"abab","Recording stopped","10:48:01 AM"
"abab","Recording started","10:49:14 AM"
"fghi","Recording stopped","10:11:40 AM"
"fghi","Recording started","10:12:13 AM"
"fghi","Recording stopped","11:13:01 AM"
"fghi","Recording started","9:14:13 AM"
"tr","Recording stopped","8:45:01 AM"
"tr","Recording started","11:14:18 AM"

然后,您可以将停止时间和开始记录时间进行合并,例如:

$csv = Import-Csv -Path 'THE PATH TO YOUR INPUT CSV FILE'

$result = $csv | Group-Object Entity | ForEach-Object {
    $name = $_.Name
    $stopTime = $null
    foreach ($entity in $_.Group) {
        if (!$stopTime) { $stopTime = $entity.Time } 
        else { 
            [PsCustomObject]@{
                'Entity' = $name
                'Recording stopped' = $stopTime
                'Recording started' = $entity.Time 
            }
            $stopTime = $null      
        }
    }
}

# output on screen
$result

# output to new CSV file
$result | Export-Csv -Path 'THE PATH TO YOUR OUTPUT CSV FILE' -NoTypeInformation

屏幕上的结果如下:

Entity Recording stopped Recording started
------ ----------------- -----------------
abab   10:48:01 AM       10:49:14 AM      
fghi   10:11:40 AM       10:12:13 AM      
fghi   11:13:01 AM       9:14:13 AM       
tr     8:45:01 AM        11:14:18 AM

由于输入文件中实体fghi有4个条目,因此将导致结果在该实体的输出中有两个条目。