Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - parsing csv files with fields that may contain commas

Reply
 
Old 07-12-2007   #1 (permalink)
Frank
Guest


 
 

parsing csv files with fields that may contain commas

Hi,

What is the best way to parse a csv comma delimeted file which may have
commas in the fields. If there are commas in the field, it is enclosed in
double quotes, ie.

field1,field2,field3
"field1, abc",field2,field3


So, in the second row, the first field would be: "field1, abc"

Thanks in advance,



My System SpecsSystem Spec
Old 07-12-2007   #2 (permalink)
Kiron
Guest


 
 

Re: parsing csv files with fields that may contain commas

You can use the Import-Csv Cmdlet to assign the contents of the csv file to
a variable, then you can retrieve the fields as properties of the variable.

$data = import-csv data.csv
$data | select-onject field1
$data | select-object field1 | format-table -hide
$data | foreach-object {$_.field1}--
Kiron

My System SpecsSystem Spec
Old 07-12-2007   #3 (permalink)
Brandon Shell
Guest


 
 

Re: parsing csv files with fields that may contain commas

I would add headers to the CSV
Here is my test file
====== TEST FILE =======
Header1,Header2,Header3
this is field1,"fieldtwo,fieldtwo",this is field 3
====== TEST FILE =======

You can then use import-csv to import for parsing
PS> $csvimporttest = import-csv test.csv
PS> $csvimporttest
Header1 Header2
Header3
------ ------
------
this is field1 fieldtwo,fieldtwo
this is field 3

As you can see... the quotes worked as expected.

Then to parse like this
===== CODE =====
$csvimporttest = import-csv test.csv
foreach($object in $csvimporttest)
{
write-host $object.header1
write-host $object.header2
write-host $object.header3
}
===== CODE =====

"Frank" <Frank@discussions.microsoft.com> wrote in message
news:FED131FD-F83D-4E0B-BE0E-7C314E314855@microsoft.com...
> Hi,
>
> What is the best way to parse a csv comma delimeted file which may have
> commas in the fields. If there are commas in the field, it is enclosed in
> double quotes, ie.
>
> field1,field2,field3
> "field1, abc",field2,field3
>
>
> So, in the second row, the first field would be: "field1, abc"
>
> Thanks in advance,
>
>


My System SpecsSystem Spec
Old 07-12-2007   #4 (permalink)
Frank
Guest


 
 

Re: parsing csv files with fields that may contain commas

I seem to get errors unless all fields are there, for example in my first
line, I have the right amount of commas but there is no third field:

field1,"field 2,sfd",
field1,field2,field3

I would get the error:

$test =import-csv test.csv
Import-Csv : Cannot process argument because the value of argument "name" is
invalid. Change the value of the "name" argument and run the operation again.
At line:1 char:18
+ $test =import-csv <<<< test.csv


I have verified that if there was a third field, it would work. Is there a
way around this?

Thanks,



"Brandon Shell" wrote:

> I would add headers to the CSV
> Here is my test file
> ====== TEST FILE =======
> Header1,Header2,Header3
> this is field1,"fieldtwo,fieldtwo",this is field 3
> ====== TEST FILE =======
>
> You can then use import-csv to import for parsing
> PS> $csvimporttest = import-csv test.csv
> PS> $csvimporttest
> Header1 Header2
> Header3
> ------ ------
> ------
> this is field1 fieldtwo,fieldtwo
> this is field 3
>
> As you can see... the quotes worked as expected.
>
> Then to parse like this
> ===== CODE =====
> $csvimporttest = import-csv test.csv
> foreach($object in $csvimporttest)
> {
> write-host $object.header1
> write-host $object.header2
> write-host $object.header3
> }
> ===== CODE =====
>
> "Frank" <Frank@discussions.microsoft.com> wrote in message
> news:FED131FD-F83D-4E0B-BE0E-7C314E314855@microsoft.com...
> > Hi,
> >
> > What is the best way to parse a csv comma delimeted file which may have
> > commas in the fields. If there are commas in the field, it is enclosed in
> > double quotes, ie.
> >
> > field1,field2,field3
> > "field1, abc",field2,field3
> >
> >
> > So, in the second row, the first field would be: "field1, abc"
> >
> > Thanks in advance,
> >
> >

>
>

My System SpecsSystem Spec
Old 07-12-2007   #5 (permalink)
Flowering Weeds
Guest


 
 

Re: parsing csv files with fields that may contain commas


"Frank"

>I seem to get errors unless all
> fields are there, for example in my first
> line, I have the right amount of commas
> but there is no third field:
>
> field1,"field 2,sfd",
> field1,field2,field3
>


Perhaps Microsoft's Log Parser 2.2

PS> LogParser.exe "SELECT field1 AS Item1 ,field2 AS Item2 , field3 AS
Item3 FRO
M testComma.csv" -i:csv -statsff -headerRowff
Item1 Item2 Item3
----------- ----------- ------
field1 field2 field3
field1, abc field2 field3
field1 field 2,sfd <NULL>
field1 field2 field3
PS>

Or perhaps:

PS> LogParser.exe "SELECT field1 AS Item1 ,field2 AS Item2 ,
REPLACE_IF_NULL(fie
ld3,'Yep blank') AS Item3 FROM
testComma.csv" -i:csv -statsff -headerRowff -
o:nat -colSep:" Field blank? "
Item1 Item2 Item3
----------- ----------- ---------
field1 Field blank? field2 Field blank? field3
field1, abc Field blank? field2 Field blank? field3
field1 Field blank? field 2,sfd Field blank? Yep blank
field1 Field blank? field2 Field blank? field3
PS>

Just another way!





My System SpecsSystem Spec
Old 07-12-2007   #6 (permalink)
Brandon Shell
Guest


 
 

Re: parsing csv files with fields that may contain commas

It should work if your header field is filled out.

header1,header2,header3
field1,"field 2,sfd",
field1,field2,field3

9# Import-Csv $pwd\test.csv

header1 header2 header3
------- ------- -------
field1 field 2,sfd
field1 field2 field3

"Frank" <Frank@discussions.microsoft.com> wrote in message
news:BADF24EE-5D76-4152-8FF6-7EFD0F47CA42@microsoft.com...
>I seem to get errors unless all fields are there, for example in my first
> line, I have the right amount of commas but there is no third field:
>
> field1,"field 2,sfd",
> field1,field2,field3
>
> I would get the error:
>
> $test =import-csv test.csv
> Import-Csv : Cannot process argument because the value of argument "name"
> is
> invalid. Change the value of the "name" argument and run the operation
> again.
> At line:1 char:18
> + $test =import-csv <<<< test.csv
>
>
> I have verified that if there was a third field, it would work. Is there
> a
> way around this?
>
> Thanks,
>
>
>
> "Brandon Shell" wrote:
>
>> I would add headers to the CSV
>> Here is my test file
>> ====== TEST FILE =======
>> Header1,Header2,Header3
>> this is field1,"fieldtwo,fieldtwo",this is field 3
>> ====== TEST FILE =======
>>
>> You can then use import-csv to import for parsing
>> PS> $csvimporttest = import-csv test.csv
>> PS> $csvimporttest
>> Header1 Header2
>> Header3
>> ------ ------
>> ------
>> this is field1 fieldtwo,fieldtwo
>> this is field 3
>>
>> As you can see... the quotes worked as expected.
>>
>> Then to parse like this
>> ===== CODE =====
>> $csvimporttest = import-csv test.csv
>> foreach($object in $csvimporttest)
>> {
>> write-host $object.header1
>> write-host $object.header2
>> write-host $object.header3
>> }
>> ===== CODE =====
>>
>> "Frank" <Frank@discussions.microsoft.com> wrote in message
>> news:FED131FD-F83D-4E0B-BE0E-7C314E314855@microsoft.com...
>> > Hi,
>> >
>> > What is the best way to parse a csv comma delimeted file which may have
>> > commas in the fields. If there are commas in the field, it is enclosed
>> > in
>> > double quotes, ie.
>> >
>> > field1,field2,field3
>> > "field1, abc",field2,field3
>> >
>> >
>> > So, in the second row, the first field would be: "field1, abc"
>> >
>> > Thanks in advance,
>> >
>> >

>>
>>


My System SpecsSystem Spec
Old 07-12-2007   #7 (permalink)
Frank
Guest


 
 

Re: parsing csv files with fields that may contain commas

Brandon,

You were absolutely correct. I guess I just have to make sure that the
headers are there. Before, the first record only had the first 2 fields.

Thanks,


"Brandon Shell" wrote:

> It should work if your header field is filled out.
>
> header1,header2,header3
> field1,"field 2,sfd",
> field1,field2,field3
>
> 9# Import-Csv $pwd\test.csv
>
> header1 header2 header3
> ------- ------- -------
> field1 field 2,sfd
> field1 field2 field3
>
> "Frank" <Frank@discussions.microsoft.com> wrote in message
> news:BADF24EE-5D76-4152-8FF6-7EFD0F47CA42@microsoft.com...
> >I seem to get errors unless all fields are there, for example in my first
> > line, I have the right amount of commas but there is no third field:
> >
> > field1,"field 2,sfd",
> > field1,field2,field3
> >
> > I would get the error:
> >
> > $test =import-csv test.csv
> > Import-Csv : Cannot process argument because the value of argument "name"
> > is
> > invalid. Change the value of the "name" argument and run the operation
> > again.
> > At line:1 char:18
> > + $test =import-csv <<<< test.csv
> >
> >
> > I have verified that if there was a third field, it would work. Is there
> > a
> > way around this?
> >
> > Thanks,
> >
> >
> >
> > "Brandon Shell" wrote:
> >
> >> I would add headers to the CSV
> >> Here is my test file
> >> ====== TEST FILE =======
> >> Header1,Header2,Header3
> >> this is field1,"fieldtwo,fieldtwo",this is field 3
> >> ====== TEST FILE =======
> >>
> >> You can then use import-csv to import for parsing
> >> PS> $csvimporttest = import-csv test.csv
> >> PS> $csvimporttest
> >> Header1 Header2
> >> Header3
> >> ------ ------
> >> ------
> >> this is field1 fieldtwo,fieldtwo
> >> this is field 3
> >>
> >> As you can see... the quotes worked as expected.
> >>
> >> Then to parse like this
> >> ===== CODE =====
> >> $csvimporttest = import-csv test.csv
> >> foreach($object in $csvimporttest)
> >> {
> >> write-host $object.header1
> >> write-host $object.header2
> >> write-host $object.header3
> >> }
> >> ===== CODE =====
> >>
> >> "Frank" <Frank@discussions.microsoft.com> wrote in message
> >> news:FED131FD-F83D-4E0B-BE0E-7C314E314855@microsoft.com...
> >> > Hi,
> >> >
> >> > What is the best way to parse a csv comma delimeted file which may have
> >> > commas in the fields. If there are commas in the field, it is enclosed
> >> > in
> >> > double quotes, ie.
> >> >
> >> > field1,field2,field3
> >> > "field1, abc",field2,field3
> >> >
> >> >
> >> > So, in the second row, the first field would be: "field1, abc"
> >> >
> >> > Thanks in advance,
> >> >
> >> >
> >>
> >>

>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
XML parsing and Win7 unattended answer files PowerShell
commas in PowerShell PowerShell
Parsing AssemblyInfo files .NET General
I am looking for information on parsing log files (text) PowerShell
Parsing log files for analysis PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46