Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Perl script help to convert .txt file to .csv

by Seabass (Novice)
on Dec 20, 2011 at 13:38 UTC ( #944392=perlquestion: print w/ replies, xml ) Need Help??
Seabass has asked for the wisdom of the Perl Monks concerning the following question:

Hello friends! Newbie to Perl, but quickly learning.

I need help with a script that will read a text file, surround fields by commas, then print it to another file.

The purpose is to prepare raw pyro-sequencing files for upload into a SQL database.

Example of the infile:
>G9JVYGV01AJE8V length=135 xy=0104_0349 region=1 run=R_2011_09_20_15_0 +0_06_ GGTGGTAGTGAAGAAGAGGAGATGAAAGTGGAAGAGGTTGAGGATGAGAAGGTTGAATTG GAAGAAGAAGATGAGAAGGTTGAAGTGGAAGATGAGAAGGTTGAAGTGGAAGAAGATGAA GTGGAAGAGAGGAGC >G9JVYGV01A4910 length=90 xy=0353_0150 region=1 run=R_2011_09_20_15_00 +_06_ GGTGCATGGCATTGTAGATGGTTGCTTGATAGTTGCCCATACGTGTACTACACTTGCAGA GTGAAGCAACCATCTACAATGCCATGCACC >G9JVYGV01A0SVP length=70 xy=0302_0163 region=1 run=R_2011_09_20_15_00 +_06_ GCACCATTCAGCACAGATATAGTAGCCACATCAACACAAGTTACCTAACTATATCTGTGC TGAATGGTGC >G9JVYGV01A221U length=89 xy=0328_0160 region=1 run=R_2011_09_20_15_00 +_06_ CTGGACATTTACATCCATAAGTAGGAGTTAGGACTCTGCACCAGCCTCTTGAGCTTGTGA CGTCTCTTCTCCTCCTCCGGACTGGGACA >G9JVYGV01BVCPK length=46 xy=0650_0134 region=1 run=R_2011_09_20_15_00 +_06_ GCAAGATCGCAAGCCAAGCAACGTTTCACGAACTGGCCAGAATGAG >G9JVYGV01AOU3I length=81 xy=0166_0220 region=1 run=R_2011_09_20_15_00 +_06_ TCATTGACATCTGTGCAGCTGCAGGAGCGGATATGAGGAGATGGTTCTATCTGCACAGAT GTCAATGAGTGTGACAGTGAT >G9JVYGV01A0JEL length=61 xy=0299_0171 region=1 run=R_2011_09_20_15_00 +_06_ CGAGTGAAGGCATTGGTGATGCTGGTGTGAAGAGTGAGGGCATCGCCAATGCCTTCACTC G

Each entry begins with a carot and fields are seperated by whitespace.

I want to replace all whitespace with commas, delete the text 'length=' from its corresponding field, remove all new lines/EOL markers, and seperate each entry with a new line.

I am new, but I figured out how to read/write files, replace whitespaces with commas, and chomp all new lines.

Here is the code I have so far:
use strict; use warnings; print "\n Running script for Jiggs \n"; my $infile=<foot.txt>; open(my $in,'<', "$infile") or die $!; open(my $out, '>' ,'foot1.txt') or die $!; my $line = <$in>; chomp($line); $line =~ s/ /,/g; print $out "$line"; $line =<$in>; print $out "$line"; while($line =<$in>) { chomp($line); $line =~ s/ /,/g; print $out "$line"; } close ($in); close ($out); print "\n Done!\n";
Here is an example of the output I want:
>G9JVYGV01AJE8V,135,xy=0104_0349,region=1,run=R_2011_09_20_15_00_06_GG +TGGTAGTGAAGAAGAGGAGATGAAAGTGGAAGAGGTTGAGGATGAGAAGGTTGAATTGGAAGAAGAAGA +TGAGAAGGTTGAAGTGGAAGATGAGAAGGTTGAAGTGGAAGAAGATGAAGTGGAAGAGAGGAGC >G9JVYGV01A4910,90,xy=0353_0150,region=1,run=R_2011_09_20_15_00_06_GGT +GCATGGCATTGTAGATGGTTGCTTGATAGTTGCCCATACGTGTACTACACTTGCAGAGTGAAGCAACCA +TCTACAATGCCATGCACC >G9JVYGV01A0SVP,70,xy=0302_0163,region=1,run=R_2011_09_20_15_00_06_GCA +CCATTCAGCACAGATATAGTAGCCACATCAACACAAGTTACCTAACTATATCTGTGCTGAATGGTGC >G9JVYGV01A221U,89,xy=0328_0160,region=1,run=R_2011_09_20_15_00_06_CTG +GACATTTACATCCATAAGTAGGAGTTAGGACTCTGCACCAGCCTCTTGAGCTTGTGACGTCTCTTCTCC +TCCTCCGGACTGGGACA >G9JVYGV01BVCPK,46,xy=0650_0134,region=1,run=R_2011_09_20_15_00_06_GCA +AGATCGCAAGCCAAGCAACGTTTCACGAACTGGCCAGAATGAG >G9JVYGV01AOU3I,81,xy=0166_0220,region=1,run=R_2011_09_20_15_00_06_TCA +TTGACATCTGTGCAGCTGCAGGAGCGGATATGAGGAGATGGTTCTATCTGCACAGATGTCAATGAGTGT +GACAGTGAT >G9JVYGV01A0JEL,61,xy=0299_0171,region=1,run=R_2011_09_20_15_00_06_CGA +GTGAAGGCATTGGTGATGCTGGTGTGAAGAGTGAGGGCATCGCCAATGCCTTCACTCG >G9JVYGV01AUKIG,119,xy=0231_0198,region=1,run=R_2011_09_20_15_00_06_GG +CCACCAGGGCTTAACTTCCTGTGCCTCACCATCACGCAGTTGTCAGAGGATCCACATTGAACAAAGTAG +CAATTCTTTCCACTCTGTGACACACCAACATTCTTATACAGCACCAGG >G9JVYGV01AJ8F7,29,xy=0113_1333,region=1,run=R_2011_09_20_15_00_06_CTG +CTTCCAAGCCTCCAACCTCTAACCAG>G9JVYGV01AMQ87,79,xy=0142_0233,region=1,ru +n=R_2011_09_20_15_00_06_AGAGTCTCCTCATTGTTCTTTCCAAGTCCTCTATTGCTGAGCCTG +GTTTCGTACCTTCTCAGCTAGGCCCTCTTTCTCT >G9JVYGV01A4W45,85,xy=0348_3895,region=1,run=R_2011_09_20_15_00_06_GCT +TCACATCTCAGAAATATAACCGCTAATGATCTGAAACAAGTTACAATCTGACATTCTGAAACCAAATGA +AAGCAGCATAAAC >G9JVYGV01A7TPA,66,xy=0382_0140,region=1,run=R_2011_09_20_15_00_06_ATG +GCTTACCTCACTGTCGATGGAGATCGAATGCAAGCGATGTCCATCGACAGTGAGGTAAGCCAT

I am using Windows 7 and Perl 5.12.4.

Your help is very much appreciated, I'll check back and update later today as I keep trying to work this out, thanks.

Comment on Perl script help to convert .txt file to .csv
Select or Download Code
Re: Perl script help to convert .txt file to .csv
by Tux (Monsignor) on Dec 20, 2011 at 13:53 UTC
    use strict; use warnings; print "\n Running script for Jiggs \n"; my $infile = "foot.txt"; # <foot.txt> is not legal q +uotation in perl5 open my $in, "<", $infile or die $!; # Don't quote a variable ag +ain open my $out, ">", "foot1.txt" or die $!; my $line = <$in>; # Is $line the header? If s +o, don't call it "line" $line =~ s/\s+\Z/\n/; # When you chomp, you don't + get a "new" newline $line =~ s/ +/,/g; print $out $line; # Don't quote variable. It +now still has a newline print $out scalar <$in>; # What is this second line? + Something special? while (<$in>) { s/\s+\Z/\n/; s/ +/,/g; s/,length=/,/g; print $out $_; } close $in; close $out; print "\n Done!\n";

    Note that this might up being invalid CSV. Use Text::CSV or Text::CSV_XS to ensure valid CSV output.


    Enjoy, Have FUN! H.Merijn
      Cool, thanks for the reply!

      Using your example, I found out there is no need for the header part at all. I removed that part of the code, and then used the s//g; to get rid of the rest of the field names.

      Here is the code:
      use strict; use warnings; print "\n Running script for Jiggs \n"; my $infile = "foot.txt"; open my $in, "<", $infile or die $!; open my $out, ">", "foot1.txt" or die $!; while (<$in>) { s/\s+\Z/\n/; s/ +/,/g; s/,length=/,/g; s/,xy=/,/g; s/,region=/,/g; s/,run=/,/g; print $out $_; } close $in; close $out; print "\n Done!\n";
      Here is the output:
      >G9JVYGV01AJE8V,135,0104_0349,1,R_2011_09_20_15_00_06_ GGTGGTAGTGAAGAAGAGGAGATGAAAGTGGAAGAGGTTGAGGATGAGAAGGTTGAATTG GAAGAAGAAGATGAGAAGGTTGAAGTGGAAGATGAGAAGGTTGAAGTGGAAGAAGATGAA GTGGAAGAGAGGAGC >G9JVYGV01A4910,90,0353_0150,1,R_2011_09_20_15_00_06_ GGTGCATGGCATTGTAGATGGTTGCTTGATAGTTGCCCATACGTGTACTACACTTGCAGA GTGAAGCAACCATCTACAATGCCATGCACC >G9JVYGV01A0SVP,70,0302_0163,1,R_2011_09_20_15_00_06_ GCACCATTCAGCACAGATATAGTAGCCACATCAACACAAGTTACCTAACTATATCTGTGC TGAATGGTGC >G9JVYGV01A221U,89,0328_0160,1,R_2011_09_20_15_00_06_ CTGGACATTTACATCCATAAGTAGGAGTTAGGACTCTGCACCAGCCTCTTGAGCTTGTGA CGTCTCTTCTCCTCCTCCGGACTGGGACA >G9JVYGV01BVCPK,46,0650_0134,1,R_2011_09_20_15_00_06_ GCAAGATCGCAAGCCAAGCAACGTTTCACGAACTGGCCAGAATGAG >G9JVYGV01AOU3I,81,0166_0220,1,R_2011_09_20_15_00_06_ TCATTGACATCTGTGCAGCTGCAGGAGCGGATATGAGGAGATGGTTCTATCTGCACAGAT GTCAATGAGTGTGACAGTGAT >G9JVYGV01A0JEL,61,0299_0171,1,R_2011_09_20_15_00_06_ CGAGTGAAGGCATTGGTGATGCTGGTGTGAAGAGTGAGGGCATCGCCAATGCCTTCACTC G >G9JVYGV01AUKIG,119,0231_0198,1,R_2011_09_20_15_00_06_ GGCCACCAGGGCTTAACTTCCTGTGCCTCACCATCACGCAGTTGTCAGAGGATCCACATT GAACAAAGTAGCAATTCTTTCCACTCTGTGACACACCAACATTCTTATACAGCACCAGG >G9JVYGV01AJ8F7,29,0113_1333,1,R_2011_09_20_15_00_06_ CTGCTTCCAAGCCTCCAACCTCTAACCAG >G9JVYGV01AMQ87,79,0142_0233,1,R_2011_09_20_15_00_06_ AGAGTCTCCTCATTGTTCTTTCCAAGTCCTCTATTGCTGAGCCTGGTTTCGTACCTTCTC AGCTAGGCCCTCTTTCTCT >G9JVYGV01A4W45,85,0348_3895,1,R_2011_09_20_15_00_06_ GCTTCACATCTCAGAAATATAACCGCTAATGATCTGAAACAAGTTACAATCTGACATTCT GAAACCAAATGAAAGCAGCATAAAC >G9JVYGV01A7TPA,66,0382_0140,1,R_2011_09_20_15_00_06_ ATGGCTTACCTCACTGTCGATGGAGATCGAATGCAAGCGATGTCCATCGACAGTGAGGTA AGCCAT

      Almost there, but I need to put a comma after the last field before the sequence. Then remove the returns within the sequence.

      If each entry starts with a carot, then I need 6 fields seperated by commas and then the new line: the opening entry, the length=, xy=, region=, run=, AAGGTTGGCC /n).

      Apologies for not being clearer, but thanks for the help so far.

Re: Perl script help to convert .txt file to .csv
by mr.nick (Chaplain) on Dec 20, 2011 at 14:01 UTC
    I assumed the line breaks in the data was unintentional and that each line began with >:
    (timtowtdi)
    use strict; use warnings; while (<DATA>) { chomp; my @parts = split /\s+/,$_; ## $parts[0] = substr $parts[0],1,-1; $parts[1] = $1 if $parts[1] =~ /length=(\d+)/; print join(",",@parts),"\n"; } __DATA__ >G9JVYGV01AJE8V length=135 xy=0104_0349 region=1 run=R_2011_09_20_15_0 +0_06_GGTGGTAGTGAAGAAGAGGAGATGAAAGTGGAAGAGGTTGAGGATGAGAAGGTTGAATTGGAAG +AAGAAGATGAGAAGGTTGAAGTGGAAGATGAGAAGGTTGAAGTGGAAGAAGATGAAGTGGAAGAGAGGA +GC >G9JVYGV01A4910 length=90 xy=0353_0150 region=1 run=R_2011_09_20_15_00 +_06_GGTGCATGGCATTGTAGATGGTTGCTTGATAGTTGCCCATACGTGTACTACACTTGCAGAGTGAA +GCAACCATCTACAATGCCATGCACC >G9JVYGV01A0SVP length=70 xy=0302_0163 region=1 run=R_2011_09_20_15_00 +_06_GCACCATTCAGCACAGATATAGTAGCCACATCAACACAAGTTACCTAACTATATCTGTGCTGAAT +GGTGC >G9JVYGV01A221U length=89 xy=0328_0160 region=1 run=R_2011_09_20_15_00 +_06_CTGGACATTTACATCCATAAGTAGGAGTTAGGACTCTGCACCAGCCTCTTGAGCTTGTGACGTCT +CTTCTCCTCCTCCGGACTGGGACA >G9JVYGV01BVCPK length=46 xy=0650_0134 region=1 run=R_2011_09_20_15_00 +_06_GCAAGATCGCAAGCCAAGCAACGTTTCACGAACTGGCCAGAATGAG >G9JVYGV01AOU3I length=81 xy=0166_0220 region=1 run=R_2011_09_20_15_00 +_06_TCATTGACATCTGTGCAGCTGCAGGAGCGGATATGAGGAGATGGTTCTATCTGCACAGATGTCAA +TGAGTGTGACAGTGAT >G9JVYGV01A0JEL length=61 xy=0299_0171 region=1 run=R_2011_09_20_15_00 +_06_CGAGTGAAGGCATTGGTGATGCTGGTGTGAAGAGTGAGGGCATCGCCAATGCCTTCACTCG

    I like using split instead as it's a little less fragile when it comes to parsing input. Edit: I guess the OP wanted to preserve the ">" at lines' beginning, which I didn't.

    mr.nick ...

Re: Perl script help to convert .txt file to .csv
by BrowserUk (Pope) on Dec 20, 2011 at 14:13 UTC

    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?

      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?

Re: Perl script help to convert .txt file to .csv
by TJPride (Pilgrim) on Dec 20, 2011 at 16:20 UTC
    How many files do you have? It might be easier to just combine them into one file, open with a text editor capable of doing regex matching / substitution, and do the two or three operations necessary.

      Not many files, but each file contains thousands of entries (file size >35,936 KB). In fact, just opening the file in my text editor and it starts to slow down. Thanks for the idea though.

Re: Perl script help to convert .txt file to .csv
by jwkrahn (Monsignor) on Dec 20, 2011 at 22:31 UTC
    Each entry begins with a carot

    There is no caret (^) character in your data.    I assume you mean the (>) greater than character?


    $ echo ">G9JVYGV01AJE8V length=135 xy=0104_0349 region=1 run=R_2011_09 +_20_15_00_06_ GGTGGTAGTGAAGAAGAGGAGATGAAAGTGGAAGAGGTTGAGGATGAGAAGGTTGAATTG GAAGAAGAAGATGAGAAGGTTGAAGTGGAAGATGAGAAGGTTGAAGTGGAAGAAGATGAA GTGGAAGAGAGGAGC >G9JVYGV01A4910 length=90 xy=0353_0150 region=1 run=R_2011_09_20_15_00 +_06_ GGTGCATGGCATTGTAGATGGTTGCTTGATAGTTGCCCATACGTGTACTACACTTGCAGA GTGAAGCAACCATCTACAATGCCATGCACC >G9JVYGV01A0SVP length=70 xy=0302_0163 region=1 run=R_2011_09_20_15_00 +_06_ GCACCATTCAGCACAGATATAGTAGCCACATCAACACAAGTTACCTAACTATATCTGTGC TGAATGGTGC >G9JVYGV01A221U length=89 xy=0328_0160 region=1 run=R_2011_09_20_15_00 +_06_ CTGGACATTTACATCCATAAGTAGGAGTTAGGACTCTGCACCAGCCTCTTGAGCTTGTGA CGTCTCTTCTCCTCCTCCGGACTGGGACA >G9JVYGV01BVCPK length=46 xy=0650_0134 region=1 run=R_2011_09_20_15_00 +_06_ GCAAGATCGCAAGCCAAGCAACGTTTCACGAACTGGCCAGAATGAG >G9JVYGV01AOU3I length=81 xy=0166_0220 region=1 run=R_2011_09_20_15_00 +_06_ TCATTGACATCTGTGCAGCTGCAGGAGCGGATATGAGGAGATGGTTCTATCTGCACAGAT GTCAATGAGTGTGACAGTGAT >G9JVYGV01A0JEL length=61 xy=0299_0171 region=1 run=R_2011_09_ +20_15_00_06_ CGAGTGAAGGCATTGGTGATGCTGGTGTGAAGAGTGAGGGCATCGCCAATGCCTTCACTC G " | perl -e' my $line; while ( <> ) { chomp; s/length=//; tr/ /,/s; if ( /^>/ ) { print "$line\n" if $line; $line = $_; } else { $line .= $_; } } print "$line\n"; ' >G9JVYGV01AJE8V,135,xy=0104_0349,region=1,run=R_2011_09_20_15_00_06_GG +TGGTAGTGAAGAAGAGGAGATGAAAGTGGAAGAGGTTGAGGATGAGAAGGTTGAATTGGAAGAAGAAGA +TGAGAAGGTTGAAGTGGAAGATGAGAAGGTTGAAGTGGAAGAAGATGAAGTGGAAGAGAGGAGC >G9JVYGV01A4910,90,xy=0353_0150,region=1,run=R_2011_09_20_15_00_06_GGT +GCATGGCATTGTAGATGGTTGCTTGATAGTTGCCCATACGTGTACTACACTTGCAGAGTGAAGCAACCA +TCTACAATGCCATGCACC >G9JVYGV01A0SVP,70,xy=0302_0163,region=1,run=R_2011_09_20_15_00_06_GCA +CCATTCAGCACAGATATAGTAGCCACATCAACACAAGTTACCTAACTATATCTGTGCTGAATGGTGC >G9JVYGV01A221U,89,xy=0328_0160,region=1,run=R_2011_09_20_15_00_06_CTG +GACATTTACATCCATAAGTAGGAGTTAGGACTCTGCACCAGCCTCTTGAGCTTGTGACGTCTCTTCTCC +TCCTCCGGACTGGGACA >G9JVYGV01BVCPK,46,xy=0650_0134,region=1,run=R_2011_09_20_15_00_06_GCA +AGATCGCAAGCCAAGCAACGTTTCACGAACTGGCCAGAATGAG >G9JVYGV01AOU3I,81,xy=0166_0220,region=1,run=R_2011_09_20_15_00_06_TCA +TTGACATCTGTGCAGCTGCAGGAGCGGATATGAGGAGATGGTTCTATCTGCACAGATGTCAATGAGTGT +GACAGTGAT >G9JVYGV01A0JEL,61,xy=0299_0171,region=1,run=R_2011_09_20_15_00_06_CGA +GTGAAGGCATTGGTGATGCTGGTGTGAAGAGTGAGGGCATCGCCAATGCCTTCACTCG

      Yes you are correct, my mistake

      By the way, thanks for the code!

      It works great, here is the complete version that reads from and writes to files.
      use strict; use warnings; print "\n Running script for Jiggs \n"; my $infile = "foot.txt"; open my $in, "<", $infile or die $!; open my $out, ">", "foot1.txt" or die $!; my $line; while ( <$in> ) { chomp; s/length=//; s/xy=//g; s/region=//g; s/run=//g; tr/ /,/s; if ( /^>/ ) { print $out "$line\n" if $line; $line = $_; } else { $line .= $_; } } print $out "$line\n"; close $in; close $out; print "\n Done!\n";

      Many thanks to all the responses, I learned so much today: Using RegEx's and substitution, file handling, split function, and joining arrays!

        try this code ..

        use strict; use warnings; open ( my $in ,"intxt.txt") || die "check the input file...not able t +o open $!"; open ( my $out ,">output.txt") || die "check the input file...not abl +e to open $!"; $/=""; my $in_whole= <$in>; $in_whole=~ s/\n//g; my @arr=split(">",$in_whole); my $fout=join(">",@arr); print $fout; $fout=~ s/>/\n>/g; $fout=~ s/^(\n)//; $fout=~ s/\s/,/g; $fout=~ s/length=//g; @arr=split(">",$fout); $fout=join(">",@arr); $fout=~ s/>/\n>/g; $fout=~ s/^(\n)//; $fout=~ s/,\n/\n/g; print $out $fout; close($in); close($out);
        Hi, The above code is not working for me. My requirement is to convert tab delimited file to CSV file. I am pasting below records 0000709a1470a60284e3b243f713f5cb NULL 70304222 14165222 235588862 1 00010cc71380a0aa17070f07fc36a6be 734255876 70303022 14165222 235588862 1 000142841420a2a805916731fc6bad60 125729237 70303022 14165222 235588862 1 000144231480a5f162664e26ff3ca308 NULL 70304222 14165222 235588862 1 I want to replace tabs as comma.I will appreciate your help if any one can reply ASAP

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://944392]
Approved by ww
Front-paged by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-12-19 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls