2015-02-06 73 views
0

我有一個的.csv文件看起來像:PowerShell的:合併多行到一個

employeenumber;phone;mobile;fax;userid;Email 
99999991;+1324569991;+234569991;+5234569991;user01;[email protected] 
99999992;+1234569992;+234569992;;user02;[email protected] 
99999993;+1234569993;+234569993;;user03;[email protected] 
99999993;+12345699933;;;user03;[email protected] 
99999993;;;+5234569993;user03;[email protected] 
99999994;+1234569994;;;user04;[email protected] 

正如你可以看到有不同的employeenumbers和一些線與同employeenumber

有沒有什麼辦法可以在powershell中合併僱用號碼

類似的輸出:

employeenumber;phone;mobile;fax;userid;Email 
99999991;+1324569991;+234569991;+5234569991;user01;[email protected] 
99999992;+1234569992;+234569992;;user2;[email protected] 
99999993;+1234569993/+12345699933;+234569993;+5234569993;user03;[email protected] 
99999994;+1234569994;;;user04;[email protected] 

謝謝

回答

1

我在它採取了一槍。我相信我的回答比Mjolinor更容易閱讀。

根據使用Group-Object命令,我將CSV中的條目分組到$ singletons或$ duplicates。然後,我管理$重複項,並使用'/'字符合並在手機,移動或傳真字段中找到的記錄,如您所示。

#$csv = get-content .\CSVNeedstoMerge.csv 
$csvValues = $csv | ConvertFrom-Csv -Delimiter ';' 
$duplicates = $csvValues | group-object EmployeeNumber | ? Count -gt 1 
$objs = New-Object System.Collections.ArrayList 

$singletons = $csvValues | group-object EmployeeNumber | ? Count -eq 1 | %  {$objs.Add($_.Group)} 

ForEach ($duplicate in $duplicates){ 
$objs.Add([pscustomobject]@{employeenumber=($duplicate.Group.employeenumber | select -Unique) -as [int]; 
    phone=($duplicate.Group.phone | ? Length -gt 0) -join '/'; 
    mobile=($duplicate.Group.mobile| ? Length -gt 0) -join '/'; 
    fax=($duplicate.Group.fax | ? Length -gt 0) -join '/'; 
    userid = $duplicate.Group.userid | select -Unique 
    email= $duplicate.Group.email | select -Unique }) 
} 

$objs | Sort EmployeeNumber 
+0

非常感謝。這個解決方案很好。 – 2015-02-06 18:10:16

+0

我該如何忽略帶有空僱傭號碼的行? – 2015-02-06 18:22:12

+0

要獲得只有那些有僱傭號的人,請將$ csvValues行更改爲:$ csvValues = $ csv | ConvertFrom-Csv -Delimiter';' | ? employeeNumber' – FoxDeploy 2015-02-07 01:11:12

1

我給這一個鏡頭:

(@' 
employeenumber;phone;mobile;fax;userid;Email 
99999991;+1324569991;+234569991;+5234569991;user01;[email protected] 
99999992;+1234569992;+234569992;;user02;[email protected] 
99999993;+1234569993;+234569993;;user03;[email protected] 
99999993;+12345699933;;;user03;[email protected] 
99999993;;;+5234569993;user03;[email protected] 
99999994;+1234569994;;;user04;[email protected] 
'@).split("`n") | 
foreach {$_.trim()} | sc test.csv 

$ht = @{} 
$props = (Get-Content test.csv -TotalCount 1).split(';') 

import-csv test.csv -Delimiter ';' | 
foreach { 
if ($ht.ContainsKey($_.employeenumber)) 
    { 
    foreach ($prop in $props) 
    { 
     if ($_.$prop) 
     {$ht[$_.employeenumber].$prop = $_.$prop } 
    } 
    } 

    else { $ht[$_.employeenumber] = $_ } 
} 

$ht.values | sort employeenumber 



employeenumber : 99999991 
phone   : +1324569991 
mobile   : +234569991 
fax   : +5234569991 
userid   : user01 
Email   : [email protected] 

employeenumber : 99999992 
phone   : +1234569992 
mobile   : +234569992 
fax   : 
userid   : user02 
Email   : [email protected] 

employeenumber : 99999993 
phone   : +12345699933 
mobile   : +234569993 
fax   : +5234569993 
userid   : user03 
Email   : [email protected] 

employeenumber : 99999994 
phone   : +1234569994 
mobile   : 
fax   : 
userid   : user04 
Email   : [email protected] 
+0

Hy,謝謝你的解決方案,但一些jobseenumbers有超過1個phonenumber。 – 2015-02-06 15:36:14

+0

我明白了。你想要所有的實例,還是需要重新設計? – mjolinor 2015-02-06 16:13:28

+0

全部像:99999993; +1234569993/+12345699933 – 2015-02-06 17:27:18