Quantcast
Channel: Count the maximum character length for all the data fields in a simplified csv file and output to txt - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 5

Answer by jubilatious1 for Count the maximum character length for all the data fields in a simplified csv file and output to txt

$
0
0

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:

  1. An array is declared in a BEGIN block,
  2. 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,
  3. Within the body of the block/loop, each line is read-in, split on commas, and each resultant element is mapped into to obtain the number of chars (characters). These are pushed onto @a array,
  4. 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 then map into elements at each array position, and obtain the max. 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


Viewing all articles
Browse latest Browse all 5

Trending Articles