Reading Comma delimited, Quoted String records 
Author Message
 Reading Comma delimited, Quoted String records

[ comp.lang.perl is a non-group. It does not exist. It should not
  be posted to any more. Ask your System Administrator to remove
  it from your news server. Newsgroups and Followups trimmed
  appropriately.
]

[ not an awk question, so comp.lang.awk trimmed from followups ]

[ not a SCO question, so comp.unix.sco.programmer trimmed from followups too ]


: Does anyone know of a way to read and parse comma delimited string quoted
: records with awk or perl?

Yes, someone knows. Some others have asked this question here with
such nauseating frequency, that some Perl folks collected this, and
many other, Questions that have been Asked Frequently into a
package of cumulative Perl wisdom. It is a valuable
resource. You should use it...

They call it the Perl Frequently Asked Question (FAQ) list.

Didn't do any good though, because folks won't check the FAQ for
a newsgroup before posting their question. Sad  ;-(

: I know how to read a comma delimited file (awk -F:) but if a string field
: contains a comma (eg. ...,"McKee, Sean",...) then the field is surrounded
: in double quotes.  Is there a way to deal with this?

Perl FAQ, part 4:

--------------------------------------------------------------------
=head2 How can I split a [character] delimited string except when
       inside [character]? (Comma-separated files)

Take the example case of trying to split a string that is comma-separated
into its different fields.  (We'll pretend you said comma-separated, not
comma-delimited, which is different and almost never what you mean.) You
can't use C<split(/,/)> because you shouldn't split if the comma is inside
quotes.  For example, take a data line like this:

    SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"Error, Core Dumped"

Due to the restriction of the quotes, this is a fairly complex
problem.  Thankfully, we have Jeffrey Friedl, author of a highly
recommended book on regular expressions, to handle these for us.  He
suggests (assuming your string is contained in the special variable
$_):



         "([^\"\\]*(?:\\.[^\"\\]*)*)",?  # groups the phrase inside the quotes
       | ([^,]+),?
       | ,
     }gx;

--------------------------------------------------------------------

You just wanted to do your part to ensure that the 'F' in FAQ
remains accurate, I guess...

--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming



Fri, 19 Nov 1999 03:00:00 GMT  
 Reading Comma delimited, Quoted String records

Does anyone know of a way to read and parse comma delimited string quoted
records with awk or perl?

I know how to read a comma delimited file (awk -F:) but if a string field
contains a comma (eg. ...,"McKee, Sean",...) then the field is surrounded
in double quotes.  Is there a way to deal with this?

Sean McKee



Sat, 20 Nov 1999 03:00:00 GMT  
 Reading Comma delimited, Quoted String records


comp.lang.awk,comp.lang.perl,comp.lang.perl.misc,comp.unix.sco.programmer
<URL: :">
++
++ I know how to read a comma delimited file (awk -F:) but if a string field
++ contains a comma (eg. ...,"McKee, Sean",...) then the field is surrounded
++ in double quotes.  Is there a way to deal with this?
++

It's called RTFAQ.

Abigail  --  http://www.phil.uni-sb.de/fun/jargon/RTFAQ.html
--



Sat, 20 Nov 1999 03:00:00 GMT  
 Reading Comma delimited, Quoted String records

Quote:

> Does anyone know of a way to read and parse comma delimited string quoted
> records with awk or perl?

> I know how to read a comma delimited file (awk -F:) but if a string field
> contains a comma (eg. ...,"McKee, Sean",...) then the field is surrounded
> in double quotes.  Is there a way to deal with this?

I don't know exactly what your records look like and I
don't recall the exact awk syntax off the top of my head, but
you could make your field separator be '","' or '",' or ',"'
  I think its something like this:
    FS = "(,\")|(\",)|(\",\")";

If your records look like this:
"This is a test","This is only, a test","In the event, of a "real"
emergency"

Then I think that
    FS = "(\",\")";  or something like that would work!

--
| Matt Baney          Boeing Commercial Space Company
| Resource21           (206)-393-0148                      
 "Whenever you find yourself at the bottom of a deep  
  hole the first thing you should do is stop digging"



Sat, 20 Nov 1999 03:00:00 GMT  
 Reading Comma delimited, Quoted String records


Quote:

> Does anyone know of a way to read and parse comma delimited string quoted
> records with awk or perl?

> I know how to read a comma delimited file (awk -F:) but if a string field
> contains a comma (eg. ...,"McKee, Sean",...) then the field is surrounded
> in double quotes.  Is there a way to deal with this?

> Sean McKee


Hello,

      The Perl FAQ part 4 recommends :


Assuming that $text contains the text you wish to parse.

Please read the FAQ - it is your friend.

Bob

--
I have become death, destroyer of the worlds.



Sun, 21 Nov 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Convert comma-delimited records to fixed length records

2. matching records in a comma delimited file

3. Trouble loading comma delimited records

4. getting fields NOT comma delimited with commas inside

5. parsing a comma delimited string

6. VW code to read comma-delimited text files??

7. Fortran 77: read second number on a line in a comma delimited text file

8. READ comma delimited data

9. Reading a comma delimited file

10. Parsing a CSV file containing quoted strings (themselves containing commas)

11. single-quoted string conversion to triple-quoted string

12. Parsing Comma delimited files in J

 

 
Powered by phpBB® Forum Software