Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

by Seabass (Novice)
on Dec 20, 2011 at 23:40 UTC ( #944504=note: print w/replies, xml ) Need Help??

in reply to Re: Perl script help to convert .txt file to .csv
in thread Perl script help to convert .txt file to .csv

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!

Replies are listed 'Best First'.
Re^3: Perl script help to convert .txt file to .csv
by littlemonk (Sexton) on Jan 06, 2014 at 13:48 UTC

    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);
Re^3: Perl script help to convert .txt file to .csv
by ssrao40 (Initiate) on Oct 26, 2014 at 16:08 UTC
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://944504]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-06-22 11:49 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.