Using Raku (formerly known as Perl_6)
raku -MTerminal::Table -e 'my @a = lines.map: *.split(",").list; print-table(@a);'
OR
raku -MText::Table::Simple -e 'my @a = lines.map: *.split(",").list; .say for lol2table(@a[0], @a[1..*]);'
Sample Input:
Column 1,Column 2,Column 3
1,2,3
apple,banana,cherry
Sample Output (using the Terminal::Table
module):
+--------+--------+--------+
|Column 1|Column 2|Column 3|
+--------+--------+--------+
|1 |2 |3 |
+--------+--------+--------+
|apple |banana |cherry |
+--------+--------+--------+
Sample Output (using the Text::Table::Simple
module):
O----------O----------O----------O
| Column 1 | Column 2 | Column 3 |
O==========O==========O==========O
| 1 | 2 | 3 |
| apple | banana | cherry |
----------------------------------
Above, lines
are individually split
on commas, converted to list
format, and sent to module functions for printing.
Note: it's probably best to use a dedicated CSV parser for anything other that a 'simple-CSV' file (no commas-inside-quotations, no embedded newlines, etc.). Below is an example using Raku's Text::CSV
module. Observe how fields are properly quoted in the output, and commas-within-doublequotes remain untouched:
~$ cat ./date_durian.csv
Column 1,Column 2,Column 3,Column 4
1,2,3,4
apple,banana,cherry,"date, durian"
~$ raku -MText::CSV -e 'my @a = csv(in => $*IN, sep => ",") andthen csv(in => $_, out => $*OUT);' < ./date_durian.csv
"Column 1","Column 2","Column 3","Column 4"
1,2,3,4
apple,banana,cherry,"date, durian"
~$ raku -MText::CSV -MText::Table::Simple -e 'my @a = csv(in => $*IN); .say for lol2table(@a[0], @a[1..*]);' < ./date_durian.csv
O----------O----------O----------O--------------O
| Column 1 | Column 2 | Column 3 | Column 4 |
O==========O==========O==========O==============O
| 1 | 2 | 3 | 4 |
| apple | banana | cherry | date, durian |
-------------------------------------------------
https://modules.raku.org/search/?q=CSV
https://modules.raku.org/t/TABLE
https://raku.org/