View Single Post
Old 04-10-2008   #4 (permalink)
Kiron
Guest


 

Re: Convert CSV semi-colone to comma separated

Split each line from the original CSV file on each ';', if any of the split strings matches a ',' surrounded it with double quotes and join all split strings back with a ','. Then set the modified content to the same, or to another, CSV file.
Also, if your encoding is not Unicode --PowerShell's default-- pass it to Get-Content and Set-Content statements through their -Encoding parameter.

# modifying same file, using Unicode encoding
$csvFile = <path to CSV file>
set-content $csvFile (get-content $csvFile |
% {[string]::join(',', ($_.split(';') |
% {if ($_ -match ',') {"`"$_`""} else {$_}}))})

# test it
import-csv $csvFile

--
Kiron