Discussion:
Can I use Import-CSV when separator is semicolon?
(too old to reply)
David Kriz
2008-08-04 15:51:46 UTC
Permalink
I want import CSV-file like this:

...
/------------------------------------8<----------------------------------\
Apple;124;10.04.2008
Peach;156;22.04.2008
Apricot;49;15.05.2008
\------------------------------------8<----------------------------------/
...

I use "Windows PowerShell" version 1.
Marco Shaw [MVP]
2008-08-04 15:56:11 UTC
Permalink
Post by David Kriz
...
/------------------------------------8<----------------------------------\
Apple;124;10.04.2008
Peach;156;22.04.2008
Apricot;49;15.05.2008
\------------------------------------8<----------------------------------/
...
I use "Windows PowerShell" version 1.
No, not with v1. You'll have to either try out the v2 CTP or use
another method.

Sorry, I'm short on time right now to show you another possible way with
v1... Maybe someone else will help out.

Marco
--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com
Shay Levy [MVP]
2008-08-04 19:02:28 UTC
Permalink
Hi David,

See Kiron's solution here:

http://www.microsoft.com/communities/newsgroups/list/en-us/default.aspx?dg=microsoft.public.windows.powershell&tid=c4af0b86-7444-4b35-ba90-dd47340a57b6&cat=&lang=&cr=&sloc=&p=1



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic



DK> I want import CSV-file like this:
DK>
DK> ...
DK> /------------------------------------8<-----------------------------
DK> -----\
DK> Apple;124;10.04.2008
DK> Peach;156;22.04.2008
DK> Apricot;49;15.05.2008
DK> \------------------------------------8<-----------------------------
DK> -----/
DK> ...
DK> I use "Windows PowerShell" version 1.
DK>
Kiron
2008-08-05 04:01:00 UTC
Permalink
If the ';' separated file has no header row, split each line on each ';' and
capture this array in a variable, then create a collection of PSObjects and
assign the split values to their corresponding property.
If the ';' separated file has a header row, you can modify the ';' separated
content -- and set the modified content to a temp CSV file-- by splitting
each line on each ';', if any of the split strings matches a ',' double quote
it and join all split strings back with a ',' to rebuild the line. Then you
can import the CSV data into a variable and delete the temp 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.
Thanks for the reference Shay :)

# w/o header row
$csv = gc <path to CSV file> | # <-- fullpath to the ';' separated file
% {
$val = $_.split(';')
$obj = new-object psObject
$obj | add-member noteProperty Fruit $val[0] -p |
add-member noteProperty Qty $val[1] -p |
add-member noteProperty Date $val[2] -p |
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# with header row, two versions
$csv | ft -a
# using [string]'s Join Static Method to rebuild each line
$tempCSV = [system.IO.Path]::getTempFileName()
$csvFile = <path to CSV file> # <-- full path to the ';' separated file
sc $tempCSV (gc $csvFile.path | % {[string]::join(',', ($_.split(';') |
% {if ($_ -match ',') {'"' + $_ + '"'} else {$_}}))})
# import the data
$csv = import-csv $tempCSV
# delete The temp CSV file
ri $tempCSV
$csv | ft -a

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# using PowerShell's $OFS --output field separator-- and
# string expansion on a subexpression to rebuild each line
$tempCSV = [system.IO.Path]::getTempFileName()
$csvFile = <path to CSV file> # <-- full path to the ';' separated file
& {$ofs = ','
sc $tempCSV (gc $csvFile.path | % {"$($_.split(';') |
% {if ($_ -match ',') {'"' + $_ + '"'} else {$_}})"})
}
# import the data
$csv = import-csv $tempCSV
# delete The temp CSV file
ri $tempCSV
$csv | ft -a
--
Kiron
Kiron
2008-08-05 04:12:00 UTC
Permalink
Ignore the previous posts, there were a few typos.
If the ';' separated file has no header row, split each line on each ';' and
capture this array in a variable, then create a collection of PSObjects and
assign the split values to their corresponding property.
If the ';' separated file has a header row, you can modify the ';' separated
content -- and set the modified content to a temp CSV file-- by splitting
each line on each ';', if any of the split strings matches a ',' double quote
it and join all split strings back with a ',' to rebuild the line. Then you
can import the CSV data into a variable and delete the temp 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.
Thanks for the reference Shay :)

# w/o header row
$csv = gc <path to CSV file> | # <-- fullpath to the ';' separated file
% {
$val = $_.split(';')
$obj = new-object psObject
$obj | add-member noteProperty Fruit $val[0] -p |
add-member noteProperty Qty $val[1] -p |
add-member noteProperty Date $val[2] -p
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# with header row, two versions
$csv | ft -a
# using [string]'s Join Static Method to rebuild each line
$tempCSV = [system.IO.Path]::getTempFileName()
$csvFile = <path to CSV file> # <-- full path to the ';' separated file
sc $tempCSV (gc $csvFile | % {[string]::join(',', ($_.split(';') |
% {if ($_ -match ',') {'"' + $_ + '"'} else {$_}}))})
# import the data
$csv = import-csv $tempCSV
# delete The temp CSV file
ri $tempCSV
$csv | ft -a

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# using PowerShell's $OFS --output field separator-- and
# string expansion on a subexpression to rebuild each line
$tempCSV = [system.IO.Path]::getTempFileName()
$csvFile = <path to CSV file> # <-- full path to the ';' separated file
& {$ofs = ','
sc $tempCSV (gc $csvFile | % {"$($_.split(';') |
% {if ($_ -match ',') {'"' + $_ + '"'} else {$_}})"})
}
# import the data
$csv = import-csv $tempCSV
# delete The temp CSV file
ri $tempCSV
$csv | ft -a
--
Kiron
Loading...