in reply to Perl script help to convert .txt file to .csv

This can be done with a one-liner. See perlrun for the lowdown on the switches used.

c:\test>perl -l -0x3e -ane"@F||next; s/length=//for$F[1]; @F[5]=join'' +,@F[5..99]; $#F=5; print'>',join',',@F" junk.dat >G9JVYGV01AJE8V,135,xy=0104_0349,region=1,run=R_2011_09_20_15_00_06_,G +GTGGTAGTGAAGAAGAGGAGATGAAAGTGGAAGAGGTTGAGGATGAGAAGGTTGAATTGGAAGAAGAAG +ATGAGAAGGTTGAAGTGGAAGATGAGAAGGTTGAAGTGGAAGAAGATGAAGTGGAAGAGAGGAGC >G9JVYGV01A4910,90,xy=0353_0150,region=1,run=R_2011_09_20_15_00_06_,GG +TGCATGGCATTGTAGATGGTTGCTTGATAGTTGCCCATACGTGTACTACACTTGCAGAGTGAAGCAACC +ATCTACAATGCCATGCACC >G9JVYGV01A0SVP,70,xy=0302_0163,region=1,run=R_2011_09_20_15_00_06_,GC +ACCATTCAGCACAGATATAGTAGCCACATCAACACAAGTTACCTAACTATATCTGTGCTGAATGGTGC >G9JVYGV01A221U,89,xy=0328_0160,region=1,run=R_2011_09_20_15_00_06_,CT +GGACATTTACATCCATAAGTAGGAGTTAGGACTCTGCACCAGCCTCTTGAGCTTGTGACGTCTCTTCTC +CTCCTCCGGACTGGGACA >G9JVYGV01BVCPK,46,xy=0650_0134,region=1,run=R_2011_09_20_15_00_06_,GC +AAGATCGCAAGCCAAGCAACGTTTCACGAACTGGCCAGAATGAG >G9JVYGV01AOU3I,81,xy=0166_0220,region=1,run=R_2011_09_20_15_00_06_,TC +ATTGACATCTGTGCAGCTGCAGGAGCGGATATGAGGAGATGGTTCTATCTGCACAGATGTCAATGAGTG +TGACAGTGAT >G9JVYGV01A0JEL,61,xy=0299_0171,region=1,run=R_2011_09_20_15_00_06_,CG +AGTGAAGGCATTGGTGATGCTGGTGTGAAGAGTGAGGGCATCGCCAATGCCTTCACTCG

Whilst you may not be interested in using a one-liner, especially if you need to do this regularly, it can be instructive to see the code that the one-liner produces when deparsed:

c:\test>perl -MO=Deparse -l -0x3e -ane"@F||next; s/length=//for$F[1]; +@F[5]=join'',@F[5..99]; $#F=5; print'>',join',',@F" junk.dat BEGIN { $/ = ">"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(' ', $_, 0); next unless @F; s/length=// foreach ($F[1]); @F[5] = join('', @F[5..99]); $#F = 5; print '>', join(',', @F); } -e syntax OK

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Perl script help to convert .txt file to .csv
by Seabass (Novice) on Dec 20, 2011 at 17:23 UTC
    You are correct, this needs to be run often and was hoping to use this code as a template for future applications. I am appreciative of your reply, but I am still new to one-liners.
      You are correct, this needs to be run often and was hoping to use this code as a template for future applications. I am appreciative of your reply, but I am still new to one-liners.

      Then you could cut&paste the Deparse output and use it as the basis of your own code.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        Continued from before...Now I'm trying to understand the parsed version

        BEGIN { $/ = ">"; $\ = "\n"; }

        Not sure, but does this statement define the boundaries of each line? Start with a carat and end with a new line.

        LINE: while (defined($_ = <ARGV>)) { chomp $_;

        Setting up the while loop, what does $_ = <ARGV> mean?

        our(@F) = split(' ', $_, 0); next unless @F;

        This sets the array F and splits it at each whitespace, and two other terms I dont know. What does next unless @F mean?

        s/length=// foreach ($F[1]);

        This code clears the length= string from every first column in the array.

        @F[5] = join('', @F[5..99]);

        This joins any columns past 5-99 with column 5

        $#F = 5;

        Don't know? Does this turn the array into a scalar variable set to 5 columns?

        print '>', join(',', @F);

        This is the print statement, printing the carat. Then you join each column of the array by a comma?

        I see what your saying, but where would I paste and what parts do I change? What does the first line mean?

        I'm trying to break it down so, correct me where I'm wrong.

        c:\test>perl -MO=Deparse -l -0x3e -ane

        Is this the path for my in file?

        "@F||next; s/length=//for$F[1];

        @F is setting an array to F, so $F1 would equal the first column. Then you sub out the length= using the substitutor code.

        +@F[5]=join'', @F[5..99];

        This means join all columns up to 5, and then for column 5 join any additional columns from 5..99.

        $#F=5; print'>',join',',@F" junk.dat

        Not sure on this one, what does the # mean? Then the print statement: print the carat, the join statement, and then the array F? What is junk.dat? Is this where I put the outfile name?

        I want to understand what is going on more than just a copy and paste job. This is getting long, but I'll work through the rest and post again.

        Man you are shiz! Even though I'm still shakey with the one liner protocol, your explanations of the code answered so many of my questions! That was very helpful, thanks for your time.