Powershell - Trying to calculate the average of a csv file using a function

SkullNerd

Below you can see my code. I' trying to calculate the average line by line of my csv file. The only way I have been able to do this is by using it as an array. My question is, is there a way to pass each line through the function so that I don't have to create multiple functions?

The file looks like this:

V1  V2  V3
5   9   3
5   6   2

Script:

Function Average($a) {

        Foreach ($line in $a) {
            $total = [int]$a[0].V1 + [int]$a[0].V2 + [int]$a[0].V3
        }
        return "The Average is $($total / 3)"
    }


    #Variables

    $a = (Import-Csv "Document.csv")

    #Logic

    Average
Mathias R. Jessen

You can access the properties of each line (without knowing their names) through the psobject reference:

function Get-CsvAverage
{
    param(
        [psobject[]]$Lines
    )

    # Loop through all lines of input
    foreach($Line in $Lines){
        # For each "line" object, loop through its properties
        $Numbers = $Line.psobject.Properties |ForEach-Object {
            # Attempt to cast the property's value as an integer
            $_.Value -as [int]
        }

        # Output the average for the current line
        Write-Output ($numbers | Measure-Object -Average).Average
    }
}

Then use like:

$CsvLines = Import-Csv Document.csv
Get-CsvAverage $CsvLines

For your sample input this would produce:

5.66666666666667
4.33333333333333

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python - Calculate average for every column in a csv file

Trying to export tickets/incidents from SCSM to csv file using powershell

Trying to calculate the average in an ArrayList

using panda dataframe, how to calculate average of sequence of data in csv log file?

Trying to function to calculate date from a txt file

Calculate average of every n rows from a csv file

How to calculate Average rating for each movie in R from CSV File?

The code using Arrow function dosenot work to calculate average

Function to calculate dynamic moving average using rolling() in python

Trying to return the average from 3 number using a function (JS)

Using PowerShell to convert JSON file into .CSV file?

Trying to parse a CSV file, delimited by ( , ) and using quotes

Calculate average of each column in a file

Calculate average and write it in other file

How to calculate the average of the average in PowerBI using DAX?

Pine script - Trying to calculate the average of lows in premarket

Trying to calculate the average score for a rating system

Using simple code to get the average (in Python) of an entire column in a csv file

calculate average in a table using javascript

Calculate Cumulative Average using Pandas

Calculate average of images using pointers

Calculate average using Spark Scala

Trying to make a script calculate a value (using a function) for every 24 rows

How to parse a huge file with csv data and calculate average on one of its column in plain Scala?

How to Calculate Average Time between Dates in CSV File with multiple column in Python?

Read file.csv (two columns; x and y) then calculate cumulative moving average of second column

Filter a CSV file with multiple conditions using PowerShell

Format CSV file output using powershell

Changing the Delimiter in a large CSV file using Powershell