Script to select a field in a line with the condition to another field in the line

Hello,

I'm banging my head for an easy script to select a value regarding to another value in a semicolon and (!) new line separated variable ("matrix", "multi-dimensional array")

I have a variable like this:

$text='line1;30
line2;50
line3;20'

I want the name in column 1 which has the highest value in column 2 from the same line. In the example above the result should be 'line2'.

In this example

$text2='line-a;40
line-b;20
line-c;40'

the result should be 'line-a' or(!) 'line-c'.

Regards,

June 24th, 2015 4:35pm

Something like this?

$csv = $text | ConvertFrom-Csv -Delimiter ';' -Header Line, Value
$max = ($csv.Value | Measure-Object -Maximum).Maximum
$csv | Where-Object Value -eq $max | Select-Object -ExpandProperty Line

In your second example, if you only want one of the lines you can do this:

$csv | Where-Object Value -eq $max | Select-Object -First 1 -ExpandProperty Line


Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 5:11pm

Something like this?

$csv = $text | ConvertFrom-Csv -Delimiter ';' -Header Line, Value
$max = ($csv.Value | Measure-Object -Maximum).Maximum
$csv | Where-Object Value -eq $max | Select-Object -ExpandProperty Line

In your second example, if you only want one of the lines you can do this:

$csv | Where-Object Value -eq $max | Select-Object -First 1 -ExpandProperty Line


June 24th, 2015 5:16pm

Something like this?

$csv = $text | ConvertFrom-Csv -Delimiter ';' -Header Line, Value
$max = ($csv.Value | Measure-Object -Maximum).Maximum
$csv | Where-Object Value -eq $max | Select-Object -ExpandProperty Line

In your second example, if you only want one of the lines you can do this:

$csv | Where-Object Value -eq $max | Select-Object -First 1 -ExpandProperty Line


Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 9:09pm

Something like this?

$csv = $text | ConvertFrom-Csv -Delimiter ';' -Header Line, Value
$max = ($csv.Value | Measure-Object -Maximum).Maximum
$csv | Where-Object Value -eq $max | Select-Object -ExpandProperty Line

In your second example, if you only want one of the lines you can do this:

$csv | Where-Object Value -eq $max | Select-Object -First 1 -ExpandProperty Line


June 24th, 2015 9:09pm

Thank you!
Free Windows Admin Tool Kit Click here and download it now
June 25th, 2015 2:28am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics