How to parse csv format?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to parse csv format?
# 1  
Old 07-06-2010
Question How to parse csv format?

Hi,
I have a file with 3 fields in csv format:
Code:
/tmp/foo/,MODIFY,bar/toto
"/tmp/foo, bar/","ATTRIB,ISDIR","toto, tata/foobar"

I would like to split fields in order to obtain the following:
Line1:
Code:
/tmp/foo/
MODIFY
bar/toto

Line2:
Code:
/tmp/foo, bar/
ATTRIB,ISDIR
toto, tata/foobar

Can't find my way through it!
How would you do that?
Thanks for your help.
Santiago
# 2  
Old 07-06-2010
Hi, Try This


Code:
#!/usr/bin/perl


    use Text::CSV;

    my $file = 'inpit.csv';

    my $csv = Text::CSV->new();

    open (CSV, "<", $file) or die $!;

    while (<CSV>) {
       chomp;
        if ($csv->parse($_)) {
            my @columns = $csv->fields();
            $i=0;
            print "\n\nLINE $. \n";
            while (<@columns>) {
            print "$columns[$i] \n";
            $i = $i + 1 ;
            }
        } else {
            my $err = $csv->error_input;
            print "Failed to parse line: $err";
        }
    }
    close CSV;

# 3  
Old 07-06-2010
Code:
awk '{if ($0~/"/) {split($0,a,"\",\"")} else {split($0,a,",")}}
     {for (i=1;i<=length(a);i++) {sub(/"/,"",a[i]);print a[i]}}' urfile

# 4  
Old 07-07-2010
Thanks pravin27,
I forgot to say that I'm trying to find a shell language solution.
Altough your approach is interesting, I can't make it work. Do I need to do something special?
Code:
Can't locate Text/CSV.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at parser.pl line 4.
BEGIN failed--compilation aborted at parser.pl line 4.

Thanks rdcwayx,
I like your awk approach because it can fit my needs. But I get the following error:
Code:
awk: line 2: illegal reference to array a

What did I do wrong?

---------- Post updated at 22:29 ---------- Previous update was at 19:33 ----------

Hi there,
I did it another way (inspired by rdcwayx's idea) with sed:
The whole idea is to be able to interpret the csv output of inotifywait.
Code:
inotifywait -mrc -e modify -e attrib -e moved_to -e create /tmp/ 2>"$inotify_err" | while read -r; do
	eval "$(echo "$REPLY" | sed -r 's#'\''#'\'\\\\\'\''#; s#^("([^"]*)"|([^"][^,]*)),("([^"]*)"|([^"][^,]*)),("([^"]*)"|([^"][^,]*))$#file_name='\''\2\3\8\9'\''; action='\''\5\6'\''#')"
	echo "fn=$file_name"
	echo "a=$action"
done

Thanks for your help
Santiago
# 5  
Old 07-07-2010
I test again, and seems fine.

Code:
$ awk '{if ($0~/"/) {split($0,a,"\",\"")} else {split($0,a,",")}}
>      {for (i=1;i<=length(a);i++) {sub(/"/,"",a[i]);print a[i]}}' urfile
/tmp/foo/
MODIFY
bar/toto
/tmp/foo, bar/
ATTRIB,ISDIR
toto, tata/foobar

Maybe your system is solaris, you can use nawk.
# 6  
Old 07-08-2010
Thanks rdcwayx for your interest.
My system is debian. Here is my trace:
Code:
santiago@titan:~$ cat urfile
/tmp/foo/,MODIFY,bar/toto
"/tmp/foo, bar/","ATTRIB,ISDIR","toto, tata/foobar"
"/tmp/foo, bar/",ATTRIB,"toto, tata/foobar"
santiago@titan:~$ awk '{if ($0~/"/) {split($0,a,"\",\"")} else {split($0,a,",")}} {for (i=1;i<=length(a);i++) {sub(/"/,"",a[i]);print a[i]}}' urfile
awk: line 1: illegal reference to array a
santiago@titan:~$ nawk '{if ($0~/"/) {split($0,a,"\",\"")} else {split($0,a,",")}} {for (i=1;i<=length(a);i++) {sub(/"/,"",a[i]);print a[i]}}' urfile
nawk: line 1: illegal reference to array a

How can I get the version of awk I'm using?
# 7  
Old 07-08-2010
Hi,
you have install the attached module.
1) Save this tar file in /tmp/Text-CSV-1.18.tar
2) Untar the file.
3) INSTALLATION
Go to the directory "/tmp/Text-CSV-1.18"
To install this module type the following:

perl Makefile.PL
make
make test
make install
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a script to parse data and output to csv

I am not too savvy with arrays and am assuming that what I am looking for needs arrays. This is my requirement. So I have the raw data that gets updated to a log as shown below StudentInfo: FullInfo = { Address = Newark Age = 20 Name= John } StudentInfo:... (2 Replies)
Discussion started by: sidnow
2 Replies

2. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

3. Shell Programming and Scripting

Parse csv files by their names

HI all I have multiple csv files with the names VAR1_VAR2_VAR3_VAR4.csv All the files have the same structure inside just values change. I am trying to retrieve data from those files by fixing at each time one or more VAR. I tried to write a script but I have 2 problems: 2-... (1 Reply)
Discussion started by: Jhon.c
1 Replies

4. Shell Programming and Scripting

how to parse this file and obtain a .csv or .xls

Hello Expert, I have a file in the following format: SYNTAX_VERSION 5 MONITOR "NAME_TEMPLATES" DESCRIPTION "Monitors for contents of error " INTERVAL "1m" MONPROG "script.sh NAME_TEMPLATES" MAXTHRESHOLD GEN_BELOW_RESET SEVERITY Major ... (17 Replies)
Discussion started by: Ant-one
17 Replies

5. Shell Programming and Scripting

Retaining the Unix CSV format in Excel format while exporting

Hi All, I have created a Unix Shell script whch creates a *.csv file and export it to Excel. The problem i am facing is that Users wants one of the AMOUNT field in comma separted values. Example : if the Amount has the value as 3000000 User wants to be in 3,000,000 format. This Amount format... (2 Replies)
Discussion started by: rawat_me01
2 Replies

6. Shell Programming and Scripting

Parse csv file

Hi, Our requirement is to parse the input file(.csv format). The each column in the file is delimited with comma. We need to take each column and apply some business validation rule. If data itself contains comma, then those fields are enclosed with double quotes ("). We can see this double... (7 Replies)
Discussion started by: vfrg
7 Replies

7. Shell Programming and Scripting

Parse XML file into CSV with shell?

Hi, It's been a few years since college when I did stuff like this all the time. Can someone help me figure out how to best tackle this problem? I need to parse a file full of entries that look like this: <eq action="A" sectyType="0" symbol="PGR" exch="CA" curr="VEF" sess="NORM"... (7 Replies)
Discussion started by: Pcushing
7 Replies

8. Shell Programming and Scripting

Unix Script to parse a CSV

I am writing a unix script that will parse a CSV and edit the values. My CSV looks like this 0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0 10,11,7,0,4,12,2,3,7,0,11,3,12,4,0,5,5,4,5,0,8,6,12,0,9,3,3,0,2,7,8... (16 Replies)
Discussion started by: RJ17
16 Replies

9. Shell Programming and Scripting

CSV File parse help in Perl

Folks, I have a bit of an issue trying to obtain some data from a csv file using PERL. I can sort the file and remove any duplicates leaving only 4 or 5 rows containing data. My problem is that the data contained in the original file contains a lot more columns and when I try ro run this script... (13 Replies)
Discussion started by: lodey
13 Replies

10. UNIX for Advanced & Expert Users

How to Parse a CSV file into a Different Format

Hi I have a CSV file with me in this format Currency, USD, EUR, USD, 1.00, 1.32, EUR, 0.66, 1.00, How do I transpose the file to get to the format below. currency, currency, rate USD, USD, 1.00 USD, EUR, 1.32 EUR, USD, 0.66 EUR, EUR, 1.00 Thanks for your help We are using... (2 Replies)
Discussion started by: cdesiks
2 Replies
Login or Register to Ask a Question