Nathan Stanford II
Nathan Stanford II

Reputation: 617

Import-csv with different delimiters and quotes in powershell

I have a CSV file using different quote and text delimiter characters other than the default. I know for the delimiter there is an option for a different delimiter but I cannot find out how to get rid of the quote characters.

Import-Csv 'C:\test.txt' -Delimiter "(character U+0014 is used here, won't show here)"

But the quote character is the U+00FE and I need to remove this as well so I can get the text without any special characters. I do not want to write this out to a new file. I want to import the csv into a variable so I can do some analytic's on it. For example see if a field is empty.

Any ideas?

Upvotes: 2

Views: 8560

Answers (1)

Joey
Joey

Reputation: 354356

The delimiter is not actually a problem, as you can do that with

-Delimiter "$([char]0x14)"

As for the quotes you can use a preprocessing step and then use ConvertFrom- instead of Import-CSV:

Get-Content test.txt |
    ForEach-Object { $_ -replace ([char]0xFE) } | # to remove the “quotes”
    ConvertFrom-CSV -Delimiter "$([char]0x14)"

If your lines contain embedded quotes then it needs a bit more work and probably easier just to force-quote every field:

$14 = "$([char]0x14)"
$_ -replace ([char]0xFE) -replace '"', '""' -replace "(?<=^|$14)|(?=`$|$14)", '"'

Upvotes: 6

Related Questions