Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

[Solved] Perl Question - split function with csv file

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-23-2013
WongSifu WongSifu is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 23 January 2013, 1:19 PM EST
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
[Solved] Perl Question - split function with csv file

Hi all,

I have a csv file that appears as follows:


Code:
,2013/03/26,2012/12/26,4,1,"2017/09/26,5.75%","2017/09/26,1,2018/09/26,1,2019/09/26,1,2020/09/26,1,2021/09/26,1",,,2012/12/26,

now when i use the split function like this:


Code:
my @f  = split/,/;

the split function will split the data that is within the ".

What I need to do is still split based on a "," but not look at the "," that are in the quotes.

I'm pretty stumped here. Any help would be greatly appreciated.

Last edited by radoulov; 01-23-2013 at 01:41 PM.. Reason: Marked as solved.
Sponsored Links
    #2  
Old Unix and Linux 01-23-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 27 April 2015, 3:11 AM EDT
Location: India
Posts: 1,878
Thanks: 27
Thanked 538 Times in 515 Posts

Code:
#! /usr/bin/perl
use Text::ParseWords "quotewords";
open F, "< file";

while (<F>) {
    chomp;
    @new = quotewords(",", 0, $_);
    for (@new) { print "$_ || " }
    print "\n";
}

close F;


Code:
$ ./test.pl
 || 2013/03/26 || 2012/12/26 || 4 || 1 || 2017/09/26,5.75% || 2017/09/26,1,2018/09/26,1,2019/09/26,1,2020/09/26,1,2021/09/26,1 ||  ||  || 2012/12/26 ||  ||

The Following User Says Thank You to balajesuri For This Useful Post:
PikK45 (01-23-2013)
Sponsored Links
    #3  
Old Unix and Linux 01-23-2013
WongSifu WongSifu is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 23 January 2013, 1:19 PM EST
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
works!!! thanks!!!
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl split function vishwakar Shell Programming and Scripting 3 09-16-2011 10:47 AM
split xlsx to csv using perl malikshahid85 Programming 1 08-18-2010 04:10 AM
PERL split function castle Shell Programming and Scripting 1 02-21-2010 05:28 AM
Use split function in perl chriss_58 Shell Programming and Scripting 5 06-13-2008 05:50 AM
perl split function new2ss Shell Programming and Scripting 5 06-08-2006 10:17 PM



All times are GMT -4. The time now is 10:24 AM.