Solved

Perl script multi delimiter split

Posted on 2014-04-16
2
292 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
 
LVL 1

Author Comment

by:mikeysmailbox1
ID: 40005160
Thanks this worked great
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

12 Experts available now in Live!

Get 1:1 Help Now