Solved

Perl script multi delimiter split

Posted on 2014-04-16
2
305 Views
Last Modified: 2014-04-16
Hi

I have a comma delimited file and I need to capture the third and forth fields.

Some of the forth fields have commas in them. The entire third field is has quotes around it.

Examples

1,2,my value1,my value 2,blah,blah,blah
1,2,my value1,"my value 2,my value 3",blah,blah,blah
1,2,my value1,"'my value 2','my value 3'",blah,blah,blah
1,2,my value1,"'my value 2""my value 3",blah,blah,blah
1,2,my value1,"c:""Program Files""mydir",blah,blah,blah

There should be others.


There are multiples and I am not sure how the best way is to pull out the values

Thanks

Mike
0
Comment
Question by:mikeysmailbox1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 250 total points
ID: 40005123
This should do what you want...
# split on comma
my @vals = split /,/;
# rebuild 4th value if necessary
while ($vals[3] =~ /^"/ and $vals[3] !~ /"$/) {
    $vals[3] .= ',' . splice(@vals, 4, 1);
}

Open in new window

0
 
LVL 1

Author Comment

by:mikeysmailbox1
ID: 40005160
Thanks this worked great
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question