Add multiple columns based on unique entries in other column in csv with powershell

Dale L. Houston

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!

Loïc MICHEL

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.

edited at
0

Comments

0 comments
Login to comment

Related

Add column with unique identifiers based on values from other columns in pandas

Add to CSV a timestamp column based on other columns (using bash)

PySpark add multiple columns based on categories from the other column

Add a column to Pandas DataFrame with multiple lookups based on other columns

Extract max and min values of entries in one column based on the combined values of two other columns in csv

Add column based on other columns values

Add a column based on condition met in other columns

Add a column based on all other columns in DB

Add column in Excel based on the value of other columns

Add a column based on a condition from other columns

Add a column and populate based on other columns values

pandas select rows by matching a column entry to entries in multiple other columns

Create multiple columns in R based on other column

CSV - How to add columns based on an existing column?

Add a column that is the sum of multiple other columns

PowerShell - Add variables with multiple values to CSV column

SAS Add new multiple columns based on other column's value using loop

Change the dataframe column values based on unique combination of other columns

Trying to keep values of a column based on the unique values of two other columns

Hive: Populate other columns based on unique value in a particular column

Assign new column based on unique combinations of values in other columns

add more multiple columns based on value in other columns in R

Powershell - Group and count unique values from CSV file based on a column

Separate values of one column into multiple columns based on other column

Count unique entries in third column given rows of two other columns are the same

Converting single column to multiple columns based on unique values

Create list with unique names from multiple columns, based on another column?

Segmenting a column Data into multiple columns based on unique value in R

assigning unique value to new column based on multiple columns/rows

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive