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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |