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

In reply to Re: Perl script help to convert .txt file to .csv by Tux
in thread Perl script help to convert .txt file to .csv by Seabass

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.