iceman
iceman

Reputation: 506

powershell csv capture the 1 value of three columns

I have a CSV with data presented like this:..

#date,a,b,c#
10,1,0,0
11,0,0,1
12,0,1,0
13,0,1,0
14,0,0,1
15,1,0,0
16,0,1,0
17,1,0,0
18,1,0,0
19,0,0,1

I want to capture the column values with "1" Headers A,B,C only a "1" appears once on each line... I want my output to be something along lines of

A,C,B,B,C,A,B,A,A,C

Any help is appreciated. Thanks

Upvotes: 0

Views: 220

Answers (2)

iRon
iRon

Reputation: 23788

  • Just iterate through each row ($Row) in the data table ($data)

  • Collect all the concerned columns ('a', 'b', 'c') that meet your requirements (e.g. -eq 1)

  • Return the result if there is just one column that meets the requirement

ForEach ($Row in $Data) {
    $Header = ('a', 'b', 'c').Where{ $Row.$_ -eq 1}
    If ($Header.Count -eq 1) { $Header }
}

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174815

For each record in the CSV file, you'll want to loop through each column/header names until you find one where the column value for that record is 1:

foreach($record in Import-Csv .\path\to\file.csv){
  'A','B','C' |Where-Object { $record."$_" -eq 1 } |Select-Object -First 1
}

Upvotes: 1

Related Questions