Home > Blockchain >  How to calculate data between columns in powershell table
How to calculate data between columns in powershell table

Time:11-08

Its probably a very basic thing.

but i was wondering how to calculate and sum up the result in a new column.

lets say i have a variable that holds this table below

fresh apples     eaten
------------     -----
           2         1
          71        15
          89        32

i was wondering then, how i could calculate and present this:

fresh apples     eaten     available
------------     -----     ---------
           2         1             1 
          71        15            56
          89        32            57

Any tips or help would be awesome :)

CodePudding user response:

Assuming the table in one single variable:

@”
fresh apples,eaten
2,1
7,1
89,32
“@ > C:\Informatica_POC\fruits.csv 

$fruits=import-csv C:\Informatica_POC\fruits.csv
$fruits|Select-Object 'fresh apples',eaten,@{Name='Available';Expression={$_.'fresh apples' - $_.eaten}}

enter image description here

Hope it gives you the clarity. You just need to put another column with calculations in the expression.

CodePudding user response:

You can use a calculated property

$object | Select-Object -Property 'fresh apples',
                                  eaten,
                                  @{n='available';e={$_.'fresh apples' - $_.eaten}}
  • Related