I've done some basic work with powershell over the last year, but for some reason this is evading me. Any help would be appreciated.
I have a csv file that will have several hundred entries:
Path,Data,Files
\\someserver\somepath1,100,1
\\someserver\somepath2,150,4
\\someserver\somepath1,200,5
\\someserver\somepath3,450,8
\\someserver\somepath4,200,23
\\someserver\somepath1,350,2
\\someserver\somepath2,800,9
I'd like to have the powershell script parse through the file and produce a new csv with results that show the unique paths and summed data and files values. So from the above my expected results would be (preferrably sorted by largest data value):
Path,Data,Files
\\someserver\somepath2,950,9
\\someserver\somepath1,650,8
\\someserver\somepath3,450,8
\\someserver\somepath4,200,23
I've found several answers referring to summing a single column, but I haven't figured out it out for two columns yet. Any suggestions on both code and/or technique would be greatly appreciated. Thanks!
ugly code ...
$csv=Import-Csv c:\temp\test.csv
$csv | select -Unique -expand path|%{
$path=$_
$sdata=($csv | ?{$_.path -eq $path} | measure-object data -sum).sum
$files=($csv | ?{$_.path -eq $path} | Measure-Object files -sum).sum
"$path,$sdata,$files" |out-file c:\temp\new.csv -append
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments