Using Raku (formerly known as Perl_6)
raku -e 'my @a = lines>>.split(","); my $m = @a>>.elems.max; \
.flat.join(",").put for [Z] @a>>.[0..$m-2], ("", @a>>.[$m-1]).flat;'
OR
raku -e 'my @a = lines.map: *.split(","); my $m = @a.map(*.elems).max; \
.flat.join(",").put for [Z] @a.map(*.[0..$m-2]), ("", @a.map: *.[$m-1]).flat;'
This Raku answer is a little longer but is also more of a general solution, since the number of columns per row isn't hard-coded: a file-wide max
is computed.
Briefly (both examples), lines
are read-in, each split
on ,
commas, and stored in @a
array. A max
number of elems
(columns) is computed and stored as $m
. Then Raku's [Z]
"Zip Reduction" operator is used to pull off elements from the two lists that follow it one-by-one: these elements are flat
tened and join
ed back together in the output
, such that now a ""
empty string occupies the end of the first row, and subsequent @a[$m-1]
indexed-values are moved down a row. Nota bene: Raku doesn't auto-flatten array elements, so you have to flat
ten them manually (if desired).
In the two examples I've tried to show how Raku's >>
"hyper"-operator is similar to map
ping a function/index over individual elements, either .map(*.fn)
or (at the end of a method chain) using the colon form .map: *.fn;
, which can reduce the number of parens required.
Sample Input:
10-04-2022 00:39:13,36707,1455008753,32
11-05-2022 00:39:13,36708,1555008753,26
21-05-2022 00:39:13,36708,1555408753,15
12-06-2022 00:39:13,36709,1655008753
Sample Output:
10-04-2022 00:39:13,36707,1455008753,
11-05-2022 00:39:13,36708,1555008753,32
21-05-2022 00:39:13,36708,1555408753,26
12-06-2022 00:39:13,36709,1655008753,15
Addendum: Here's another Raku approach, this time unwrapping the row/columns into individual elements, and reconstructing with rotor
. It gives the exact same result as above:
raku -e 'my @a = lines>>.split(",").flat; my @b = @a[3,7,11...*]; \
@a.=rotor(3 => 1); @a.=map(*.join(",")); @b.=unshift(""); \
.join(",").put for [Z] @a, @b;'
https://docs.raku.org/language/operators#index-entry-[]_(reduction_metaoperators)
https://docs.raku.org/language/operators#index-entry-hyper_method_call_operator
https://docs.raku.org/routine/flat