Solved

Perl script multi delimiter split

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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