Solved

Perl script multi delimiter split

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

734 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