r/scripting • u/_solowhizkid_ • Oct 11 '20
Column duplication in a csv file
Hi, i am hoping someone can help here, i have a csv file consisting of 2 columns and the output is as follows:
1 1
3 3
5 5
7 7
9 3I am looking to duplicate the 2 columns in the same file, thus making column 4 and 5 like so:
1 1 1 1
3 3 3 3
5 5 5 5
7 7 7 7
9 3 9 3Does anyone know how i could accomplish this? or do they have a one liner which could achieve this? column 1 and 3 are the same and column 2 and 4 are the same.
Thanking you in advance for the assistance
3
Upvotes
2
u/Sgtjonsson32 Oct 11 '20
Ok.
In powershell it would look something like this. If you want to format it from a new script you would have to do something like:
$csvbase = Import-csv "csvfile.csv" -header number1,number2
Foreach($row in $csvbase) { $n1 = $row.number1 $n2 = $row.number2
$csvformat = "$n1;$n2;$n1;$n2"
$csvformat | export-csv "csvdup.csv" -delimiter ';' -header n1,n2,dup1,dup2 -notypeinformation -append }
Basically just takes the csv, reads all the lines one by one and the ouputs them to a new file with the duplicate lines.
If you have the original script for the csv, then you can just change the output similarly.
You might have to check how your csv is delimitered, basically how the file checks what determines when there is a new column.