Home > database >  duplicate a column of a csv by deleting a character
duplicate a column of a csv by deleting a character

Time:11-10

I have a problem to solve in PowerShell.
I have a csv with two columns (column1 and column2) and I want to create a third column (column3) with the same information as the first column by eliminating the "dot"

column1  column2  column3  
1.         AA        1
2.         BB        2
3.         CC        3
4.         DD        4

CodePudding user response:

Use the Import-Csv cmdlet to import the data from the original file, then use Select-Object with a calculated property to create a new property corresponding to a new column, then finally export to disk again with Export-Csv:

Import-Csv path\to\input.csv |Select-Object column1,column2,@{Name='column3';Expression={$_.column1 -replace '\.$'}} |Export-Csv path\to\output.csv -NoTypeInformation

The expression $_.column1 -replace '\.$' will take the value of the column1 property on each row, and replace a literal dot `` occurring at the very end of the string with nothing (thus removing it if present, otherwise not doing anything).

CodePudding user response:

thanks for the reply, but there is something wrong with it. I find the new column but with the data values ​​(rows) empty

  • Related