Solved

Perl script multi delimiter split

Posted on 2014-04-16
2
287 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
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
 

Author Comment

by:mikeysmailbox1
ID: 40005160
Thanks this worked great
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now