Using perl:
$ echo 'Hello,Hi,Hullo,Hammers,Based,Random' |
perl -F, -le '
BEGIN { $n = shift };
for ($i=0; $i < @F; $i += $n) {
print join(",", @F[$i .. ($i + $n - 1)]);
}' 2
Hello,Hi
Hullo,Hammers
Based,Random
This uses the first argument as the number of entries printed per output line (using variable $n). STDIN and any filename arguments are used as the input.
Due to the -F, option (which implicitly enables the -a and -n options), it automatically reads each input line and splits it on commas into array @F, then iterates over the indices of the array, $n items at a time. $n elements are printed on each output line.
NOTE: use the Text::CSV module if you need to parse actual CSV with quoted fields and commas embedded in quotes rather than simple comma-delimited input.
Output with an argument of 3 instead of 2:
$ echo 'Hello,Hi,Hullo,Hammers,Based,Random' | perl -F, -le 'BEGIN{$n = shift};for($i=0;$i<@F;$i+=$n){print join(",",@F[$i..($i+$n-1)])}' 3
Hello,Hi,Hullo
Hammers,Based,Random
And again with 4:
$ echo 'Hello,Hi,Hullo,Hammers,Based,Random' | perl -F, -le 'BEGIN{$n = shift};for($i=0;$i<@F;$i+=$n){print join(",",@F[$i..($i+$n-1)])}' 4
Hello,Hi,Hullo,Hammers
Based,Random,,
Based,RandomorBased,Random,,? The former does not fulfill "specific number of fields", but the latter does not entirely come from "splitting a single line of fields". – Kamil Maciorowski Jul 01 '22 at 23:48