Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active May 29, 2018 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitfvb/e608ba175ab9c4086cfff9e258cad051 to your computer and use it in GitHub Desktop.
Save gitfvb/e608ba175ab9c4086cfff9e258cad051 to your computer and use it in GitHub Desktop.
replace delimiter in CSV files
# with parsing of csv
$inputfile = "Datentabelle 3.txt"
$outputfile = "export.csv"
$inputdelim = "`t"
$outputdelim = ','
$inputenc = "UTF8"
$outputenc = "UTF8"
rm $outputfile
Measure-Command {
Get-Content $inputfile -ReadCount 1000 -Encoding $inputenc | ConvertFrom-Csv -Delimiter $inputdelim | ConvertTo-Csv -NoTypeInformation -Delimiter "," | foreach { $_ -replace '^"','' -replace "`",`"",',' -replace '"$','' } | Set-Content -Path $outputfile -Encoding $outputenc
}
# plain replacement of chars without csv parsing
$inputfile = "Datentabelle 4.txt"
$outputfile = "export.csv"
$inputdelim = "`t"
$outputdelim = ','
$inputenc = "UTF8"
$outputenc = "UTF8"
rm $outputfile
Measure-Command {
Get-Content $inputfile -ReadCount 1000 -Encoding $inputenc | % { $_ -replace '^"','' -replace """`t""","," -replace '"$','' } | Set-Content -Path $outputfile -Encoding $outputenc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment