Reputation: 28
I've got this csv file (which has no header) :
001;0
002;7
003;192
004;72
Using powershell (for automation purpose), I need to decrement by 1 the value of the second column, so the output should be this :
001;-1
002;6
003;191
004;71
I can't find any solution without adding a header to the file (which is done with this code).
Below is where I am currently, struggling between the two pipes.
$input = "input.csv"
$output = "output.csv"
Import-CSV $input | | Export-CSV -Path $output -NoTypeInformation
Thanks !
Upvotes: 0
Views: 117
Reputation: 61028
You should not use $input
as self-defined variable, because it is an Automatic Variable in PowerShell.
This could be a way of achieving what you want:
$inputFile = "input.csv"
$outputFile = "output.csv"
$result = switch -Wildcard -File $inputFile {
'*;*' {
$item, $value = $_ -split ';', 2
'{0};{1}' -f $item, ([int]$value - 1)
}
}
$result | Set-Content -Path $outputFile
Resulting file:
001;-1 002;6 003;191 004;71
Upvotes: 2
Reputation: 1782
It's one line only:
$file = 'path to .csv'
Get-Content $file | % { $x = $_ -split ';'; $y = (([int]($x[1]))-1).ToString(); "$($x[0]);$($y)"; }
Upvotes: 0