Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Perl script help to convert .txt file to .csv

by jwkrahn (Monsignor)
on Dec 20, 2011 at 22:31 UTC ( #944489=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re: Perl script help to convert .txt file to .csv
Download Code
Re^2: Perl script help to convert .txt file to .csv
by Seabass (Novice) on Dec 20, 2011 at 23:40 UTC

    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: note [id://944489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-11-26 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (163 votes), past polls