Using Raku (formerly known as Perl_6)
~$ raku -ne 'BEGIN my @a; unless ++$ == 1 { @a.push: $_.split(",").map: *.chars; }; END say( ++$ ~ " | " ~ $_ ) for ([Z] @a).map: *.max;' file
OR:
~$ raku -ne 'BEGIN my @a; once next; @a.push: $_.split(",").map: *.chars; END say( ++$ ~ " | " ~ $_ ) for ([Z] @a).map: *.max;' file
Here's an answer coded in Raku, a member of the Perl-family of programming languages. Raku features high level support for Unicode, so that character-counting is accurate.
We start by using the (awk
-like) -ne
linewise non-autoprinting command line flags:
- An array is declared in a
BEGIN
block, - To remove the header-line (first answer), an anonymous counter (
++$
) is used to skip the first line. Alternatively (second answer),once next
can be used, - Within the body of the block/loop, each line is read-in,
split
on commas, and each resultant element ismap
ped into to obtain the number ofchars
(characters). These are pushed onto@a
array, - After all lines are read-in, the
END
block executes. The@a
array is[Z]
transformed such that rows and columns are interchanged. Once this happens we can thenmap
into elements at each array position, and obtain themax
. Finally the data is output, using an++$
anonymous counter to provide line-numbers (string-concatenation is accomplished with~
tilde).
Sample Input:
These,are,the,column_headings_which_may_be_very_long_but_they_don't_countabcdefghij,abcdefghijk,abcdefghijkl,abcaardvark,bat,cat,dogant,bee,cow,abcdefghijklm
Sample Output:
1 | 102 | 113 | 124 | 13
Note: There's no error-checking on number-of-columns per line: the [Z]
transform will simply truncate rows with an excessive number to the common (i.e. to 4 columns in the example). See the first link below to accomplish this task in Raku regardless of the number of columns per line.
https://unix.stackexchange.com/a/774828/227738
https://docs.raku.org/language/unicode
https://raku.org