Suppose, I have a source file like this.
ID|NAME|ADDRESS
1|ABC|PUNE
2|XYZA|MUMBAI
12|VB|NAGPUR
I want to get the maximum length of each column (excluding the header names). Output should be like this. 2|4|6
I have tried the command like this. tail +2 filename | cut -d"|" -f1 | awk '{ print length }' | sort -r | uniq
This works for 1st column. Is there any option available in awk to get max length for each column?
This can be a general way to do it, so that you don't have to care about the number of fields you have: store the lengths in an array and keep checking if it is the maximum or not. Finally, loop through them and print the results.
awk -F'|' 'NR>1{for (i=1; i<=NF; i++) max[i]=(length($i)>max[i]?length($i):max[i])}
END {for (i=1; i<=NF; i++) printf "%d%s", max[i], (i==NF?RS:FS)}' file
See output:
$ awk -F'|' 'NR>1{for (i=1; i<=NF; i++) max[i]=(length($i)>max[i]?length($i):max[i])} END {for (i=1; i<=NF; i++) printf "%d%s", max[i], (i==NF?RS:FS)}' a
2|4|6
For variable number of columns, we can store the maximum amount of columns in for example cols
:
$ awk -F'|' 'NR>1{cols=(cols<=NF?NF:cols); for (i=1; i<=NF; i++) max[i]=(length($i)>max[i]?length($i):max[i])} END {for (i=1; i<=cols; i++) printf "%d%s", max[i], (i==cols?RS:FS)}' a
2|4|6
This might work for you (but if there are many fields I'd use for
cycle and an array to store the length of fields...):
awk -F '|' 'NR>1 {if ( length($1) > l1 ) { l1=length($1) }
if ( length($2) > l2 ) { l2=length($2) }
if ( length($3) > l2 ) { l3=length($3) }
}
END { print l1 "|" l2 "|" l3 }' INPUTFILE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With