Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by mr.nick (Chaplain)
on Dec 20, 2011 at 14:01 UTC ( [id://944396] : note . print w/replies, xml ) Need Help??


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

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 ...