Using Raku (formerly known as Perl_6)
Converting to 2-column output:
~$ raku -e '.put for words.batch(2);' file
Converting to 3-column output:
~$ raku -e '.put for words.batch(3);' file
Raku has a words
function that splits on whitespace. Once split the elements can be batch
ed back together. You use batch
in Raku if you anticipate an incomplete/partial set of elements at the end (equivalent to rotor(partial => True)
). If you have the need to drop the final incomplete/partial set of elements at the end, use rotor()
with defaults.
Sample Input:
a aa aaa b bb bbb c cc ccc
d dd ddd e ee eee f ff fff
g gg ggg h hh hhh i ii iii
j jj jjj
Sample Output (2-column joined on \t
):
~$ raku -e '.join("\t").put for words.batch(2);' file
a aa
aaa b
bb bbb
c cc
ccc d
dd ddd
e ee
eee f
ff fff
g gg
ggg h
hh hhh
i ii
iii j
jj jjj
Sample Output (3-column joined on \t
):
~$ raku -e '.join("\t").put for words.batch(3);' file
a aa aaa
b bb bbb
c cc ccc
d dd ddd
e ee eee
f ff fff
g gg ggg
h hh hhh
i ii iii
j jj jjj
Finally, if your initial elements are not whitespace-separated, you can slurp
the file in all at once and for example split(/ \, | \n)
split on commas and newlines. See the first link below for an example.
https://unix.stackexchange.com/a/686651/227738
https://raku.org
xargs
line callecho
orprintf
? – Wildcard Sep 09 '16 at 15:59xargs
calls/bin/echo
by default – Thor Sep 09 '16 at 16:07xargs
that looks like options to/bin/echo
causes problems ... I added a warning. – Thor Sep 12 '16 at 02:46