Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

convert txt to csv

by Anonymous Monk
on Sep 14, 2009 at 16:53 UTC ( [id://795180] : perlquestion . print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I was wondering if you people have a small script to convert the following pattern from a txt file to csv. The pattern would be as following, sometime the rows might change in numbers but sequence would be static.
dn: uid=OF6206,ou=INTERNAL,ou=PEOPLE uid: of6206 epsNotesid: CN=Ai dn: uid=rg7180,ou=Internal,ou=Disabled uid: rg7180 dn: uid=io8657,ou=Internal,ou=Disabled uid: io8657 dn: uid=AO5351,ou=INTERNAL,ou=PEOPLE uid: ao5351 epsNotesid: CN=Ri
Thanks, Pamela

Replies are listed 'Best First'.
Re: convert txt to csv
by bv (Friar) on Sep 14, 2009 at 17:30 UTC

    Have you looked at Text::CSV?

    In order for us to best help you, you should try to solve the problem yourself first, then ask questions if you get stumped. Don't just expect us (or any other Internet forum) to just write your code for you.

    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))
      My Respected bv, You are mistaken, please go through my english sentence, i said if someone "has" and i didnt ask for anyone to "write". What i am looking for a hint, which would help me to write a script. Thanks, Pamela

        Pamela,

        If you are looking for a hint, then assuming your data is LDIF, then searching CPAN for LDIF will probably give several very helpful modules.

        print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))
Re: convert txt to csv
by bichonfrise74 (Vicar) on Sep 15, 2009 at 01:04 UTC
    Try something like this. Although I think Text::CSV might do a better job.
    #!/usr/bin/perl use strict; my @new_data; my @data = <DATA>; for my $i (0 .. $#data) { ( $data[$i] !~ /^$/ ) ? $data[$i] =~ s/(.*)\n/\"$1\",/g : chop $new_data[-1]; push( @new_data, $data[$i] ); } print @new_data; __DATA__ dn: uid=OF6206,ou=INTERNAL,ou=PEOPLE uid: of6206 epsNotesid: CN=Ai dn: uid=rg7180,ou=Internal,ou=Disabled uid: rg7180 dn: uid=io8657,ou=Internal,ou=Disabled uid: io8657 dn: uid=AO5351,ou=INTERNAL,ou=PEOPLE uid: ao5351 epsNotesid: CN=Ri