4

I was having a really hard time describing what I need in the Title, so I apologize ahead of time if that makes absolutely no sense.

If I have a CSV that has 2 columns, one with a persons name and a second column with a numeric value I need to find the duplicates in the names column then add the numeric values for that person together to get a total number in a new CSV.

This is a very simplified version of the real CSV

Name,Number
Dog,1
Cat,2
Fish,1
Dog,3
Dog,2
Cat,2
Fish,1

Given the information above, what I would like to be able to produce is this:

Name,Number
Dog,6
Cat,4
Fish,2

I really don't have any idea how to get there or if it's possible with PowerShell. I can only get as far as using group-object to group by name, but I have no clue how to add the columns after that.

The biggest problem I'm coming across with my research on this is that most if not all the results I get when googling involve adding new columns to a csv and not performing the mathematical calculation.

1

1 Answer 1

5

I finally got it

$csvfile = import-csv c:\csvfile.csv

$csvfile | group name | select name,@{Name="Totals";Expression={($_.group | Measure-Object -sum number).sum}} 

Credit goes to:

http://www.hanselman.com/blog/ParsingCSVsAndPoorMansWebLogAnalysisWithPowerShell.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.