Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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!


Comment on Re^2: Perl script help to convert .txt file to .csv
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://944504]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-12-28 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (177 votes), past polls