Sample Header Ad - 728x90

Determine maximum column length for every column in a simplified csv-file (one line per row)

8 votes
4 answers
12373 views
To determine the maximum length of each column in a comma-separated csv-file I hacked together a bash-script. When I ran it on a linux system it produced the correct output, but I need it to run on OS X and it relies on the GNU version of wc that can be used with the parameter -L for --max-line-length. The version of wc on OSX does not support that specific option and I'm looking for an alternative. My script (which not be that good - it reflects my poor scripting skills I guess): #!/bin/bash for((i=1;i< head -1 $1|awk '{print NF}' FS=,+1 ;i++)); do echo | xargs echo -n "Column$i: " && cut -d, -f $i $1 |wc -L ; done Which prints: Column1: 6 Column2: 7 Column3: 4 Column4: 4 Column5: 3 For my test-file: 123,eeeee,2323,tyty,3 154523,eegfeee,23,yty,343 I know installing the GNU CoreUtils through Homebrew might be a solution, but that's not a path I want to take as I'm sure it can be solved without modifying the system.
Asked by jpw (183 rep)
Sep 4, 2014, 07:55 AM
Last activity: Jun 22, 2025, 04:39 PM