Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

adding up values of a CSV column according to applied filter

$
0
0

Hello experts!

I have two csv files with multiple columns eacg (only two which are of importance) and have filtered and grouped the MID column (Machine-ID). Now each MID has a corresponding GB Storage Column whos values I want to add up for each group. I hope this was not too confusing. Here´s the example:

MID                  |       GB Storage
A00001FE8        |        660
A00001FE8_SQL |         75
B0001458          |       1230

and so on.... so I´ve grouped and filtered the MIDs already so A00001FE8 and A00001FE8_SQL are in one group. However now I also need to add up their Storage values. In this example 735 GB. How do I accomplish that?

My code so far:

#converting the .xls files to .csv for import
Function makeCSV($Excelfilename, $CSVfilename){
    $xlCSV=6
    $Excel = New-Object -comobject Excel.Application
    $Excel.Visible = $False
    $Excel.displayalerts=$False
    $Workbook = $Excel.Workbooks.Open($ExcelFileName)
    $Workbook.SaveAs($CSVfilename,$xlCSV)
    $Excel.Quit()
  If(ps excel){
    kill -name excel
    }
}

#call function to convert desired files to .csv
#param1 = filename and path to .xls file
#param2 = filename and path to .csv file that will be created

makeCSV "c:\test.xlsx" "c:\test.csv"
makeCSV "c:\test2.xlsx" "c:\test2.csv"


#import first csv file and multiply the GB TSM Storage values by 100
$csv = Import-Csv c:\test.csv" -Delimiter ";"   


foreach($csvObj in $csv ){

    $csvObj.'GB TSM Storage'=100*($csvObj.'GB TSM Storage').replace(',','.')
    

}

$csv | Sort-Object MID | Format-Table MID, 'GB TSM Storage' -AutoSize

$csv | group {$_.MID.substring(0,9)}

...and the same for the other csv file. Basically the ultimate goal is to compare the values of the summed up GB rows of each file where the MID corresponds. But for now all I need is to sum up said values.

I´m a beginner so if you find any errors in my code or improvement-suggestions feel free to critisize.

many thanks in advance


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>