Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a value in column of a CSV file

Tags:

powershell

csv

I'm trying to find a way to change the value of all the entries in the 'Available Person' column in my csv file using PowerShell.

If the value is 'Y' it should be changed to '1' and if the value is 'N' it should be changed to ')':

Branch Number,      CoreID,     Available Person,      Workstation
8002,           FMD354800200,   Y,
8002,           FMD354800201,   Y,
8002,           FMD354800202,   N,
8002,           FMD354800203,   N,
8002,           FMD354800204,   Y,

Here's what I've tried:

$csv=Import-Csv user.csv' | $csv | %{ if($_.'Available Person' -eq "Y") {$_.'Available Person'="1"} } $csv|export-csv user1.csv' -NoTypeInformation
like image 456
Paddy Avatar asked Dec 15 '22 01:12

Paddy


2 Answers

Here is some example code for how you might solve this with a Switch statement

$ImportedCSV = Import-CSV C:\user.csv
$NewCSV = Foreach ($Entry in $ImportedCsv) {
    Switch ($Entry."Available Person") {
        Y {$Entry."Available Person" = "1"}
        N {$Entry."Available Person" = ")"}
        default {Write-Error "$($Entry."Branch Number") has unexpected value for Available Person"}
    }
    $Entry
}
$NewCSV | Export-CSV C:\user1.csv -NoTypeInformation
like image 164
BenH Avatar answered Jan 01 '23 23:01

BenH


try this

(Import-Csv C:\temp\LastAnalyse.csv | 
    %{$_.'Available Person'=if ($_.Path -eq 'Y') {'1'} elseif ($_.Path -eq 'N') {')'} else {$_.'Available Person'}; $_}) | 
        Export-Csv  C:\temp\LastAnalyse.csv -NoTypeInformation
like image 29
Esperento57 Avatar answered Jan 01 '23 22:01

Esperento57