Home > Back-end >  Split .csv by comma but skip one comma (Powershell)
Split .csv by comma but skip one comma (Powershell)

Time:03-23

I am novice in Powershell and still haven't found a solution to my issue on stackoverflow but I still think PS is the best tool for this purpose.

I have a .csv file and it has to be splitted into separate columns by comma, but the first comma has to be skipped. It looks like this:

enter image description here

The goals is to have 3 columns: "Name, Surname (Link Personal)", "SSO", and "Access"

enter image description here

If I am not mistaken, the way to split it into every single comma would be:

import-csv \\file.csv | export-csv -Delimiter "," -path \\updated_file.csv

CodePudding user response:

You can use a calculated property to concatenate the two first columns into one

Import-Csv -Path \\file.csv | 
    Select-Object -Property @{Name = 'Name, Surname (Link Personal)'; Expression= {"$($_.Name), $($_.'Surname (Link Personal)')"}}, SSO, Access |
        Export-Csv -Path \\updated_file.csv -Delimiter ',' 

And BTW: When you post sample data you should post them as text formatted as code. So people willing to help you can copy and play around with them. ;-)

  • Related