G'day csorrentini,

"This is what I currently have as far as reading in the text file and writing the csv file."

I concur with earlier comments regarding the use of Text::CSV.

"My problem is I have no idea how I would go about grabbing data into specific fields with the way the txt file is currently formatted."

I would probably just read the lines in groups of three. Here's my take on what (the guts of) your code might look like:

#!/usr/bin/env perl -l use strict; use warnings; use autodie; use constant INFO_LINES => 3; use Text::CSV; my $txt_in = 'pm_1092407_txt_in.txt'; my $csv_out = 'pm_1092407_csv_out.csv'; cat_file($txt_in, 'INPUT'); my $csv = Text::CSV::->new; open my $txt_in_fh, '<', $txt_in; open my $csv_out_fh, '>', $csv_out; my @captures; while (<$txt_in_fh>) { chomp; push @captures, $_; unless ($. % INFO_LINES) { $csv->print($csv_out_fh, [@captures]); @captures = (); } } close $txt_in_fh; close $csv_out_fh; cat_file($csv_out, 'OUTPUT'); sub cat_file { my ($file, $prompt) = @_; $prompt = 'FILE' unless defined $prompt; print '=' x 64; print "$prompt ($file):"; print '-' x 64; system cat => $file; }

Output:

================================================================ INPUT (pm_1092407_txt_in.txt): ---------------------------------------------------------------- Jacobs, April 750.467.9582 quam.quis@sedhendrerit.org Mays, Martena 870.348.1974 sollicitudin@nonummyFusce.org McNeil, Brennan 289.527.6151 lobortis@nisl.com Sexton, Melvin 599.927.5337 in.felis@varius.com Blackburn, Prescott 661.589.1228 sed@egetlaoreetposuere.edu ================================================================ OUTPUT (pm_1092407_csv_out.csv): ---------------------------------------------------------------- "Jacobs, April",750.467.9582,quam.quis@sedhendrerit.org "Mays, Martena",870.348.1974,sollicitudin@nonummyFusce.org "McNeil, Brennan",289.527.6151,lobortis@nisl.com "Sexton, Melvin",599.927.5337,in.felis@varius.com "Blackburn, Prescott",661.589.1228,sed@egetlaoreetposuere.edu

-- Ken


In reply to Re: converting txt to csv and formatting by kcott
in thread converting txt to csv and formatting by csorrentini

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.