I suspect an XY problem, but here is a 15-minute (5 minutes to write the script, 10 minutes to explain it) solution for the problem as stated:

#!/usr/bin/perl use strict; use warnings; my @record = (); # record accumulator my $row = 1; # record counter while (<DATA>) { chomp; s/,,,$//; if (m/^$/) { print join(',', $row++, @record), ",,\n"; @record = (); # reset for next record } else { push @record, $_; } } __DATA__ Eli Stern,,, 10-Nov-19,,, 1 subscriber,,, ,,, Abdessamed Ham,,, 15-Nov-19,,, 0 subscribers,,, ,,, Arne Martinson,,, 22-Nov-19,,, 0 subscribers,,, ,,,

For the simplest option, just paste your real data in after the __DATA__ line instead of your sample data and run the script. Note that you may need to add use utf8; to the script code if your data is in UTF-8, since that pragma will also set the encoding for the DATA filehandle, but encoding should not be a problem because the script treats its input as mostly-opaque byte strings.

For a slightly more complex option, put all the input into a file, delete __DATA__ and everything after it, and change while (<DATA>) to while (<>), then run as "perl ./script.pl input.txt > output.txt".

The script works by collecting fields (into the @record array) until it sees an empty line, then joining together an incrementing counter and the contents of the fields, printing that with a ",,\n" sequence to end the line, and clearing the record buffer to start accumulating the next record. The documentation for chomp and push are likely to be informative, as are the perlre and perlretut pages. This script uses the implicit assignment to $_ that the <> I/O read operator provides when used as a loop test and the magic DATA filehandle for reading the tail of the script file.


In reply to Re: 20 years confused by jcb
in thread 20 years confused by OrdinaryFIlmmaker

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.