Created
June 11, 2018 12:29
-
-
Save ThinGuy/876ee295a08b3527f18fe37e6f70d72b to your computer and use it in GitHub Desktop.
awk one liner: get max column lengths across all records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This will print the length of the longest field in a column across all records | |
# awk -F"|" '{ for (i=1; i<=NF; i++) { max[i] = length($i) > max[i] ? length($i) : max[i] ;ncols = i > ncols ? i : ncols }} END { for (col=1; col <= ncols; col++) {printf "%d\n", max[col]}}' | |
##This example has pipe separated record adjust field separator as needed | |
##This example only prints the max length of each column (in order) with a CR, adjust printf statement as needed | |
### Sample data #### | |
FOO=' | |
Host|SysID|DevID|Name|Model|Part ID|UUID|LABEL|FS|MNT|Bootable | |
node01ob20|pk3fqr|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node01ob20|pk3fqr|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node02ob20|ahtaey|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node02ob20|ahtaey|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node03ob20|tkaccf|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node03ob20|tkaccf|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node04ob20|akxxxe|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node04ob20|akxxxe|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node05ob20|decwdb|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node05ob20|decwdb|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node06ob20|xfsypq|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node06ob20|xfsypq|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node07ob20|cp4cnf|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node07ob20|cp4cnf|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node08ob20|m4a3ae|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node08ob20|m4a3ae|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node09ob20|nfwc3r|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node09ob20|nfwc3r|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
node10ob20|4d68wq|73|sda|KINGSTON SV300S3|48|f87c08cb-c444-4f81-af8a-5ed22947f63d|efi|fat32|/boot/efi|true | |
node10ob20|4d68wq|73|sda|KINGSTON SV300S3|49|3a8024f2-0aa0-4005-8400-cf8d4a3d175d|root|ext4|/|false | |
' | |
# Example using above data. | |
$ echo "$FOO"|awk -F"|" '{ for (i=1; i<=NF; i++) { max[i] = length($i) > max[i] ? length($i) : max[i] ;ncols = i > ncols ? i : ncols }} END { for (col=1; col <= ncols; col++) {printf "%d\n", max[col]}}' | |
10 | |
6 | |
5 | |
4 | |
16 | |
7 | |
36 | |
5 | |
5 | |
9 | |
8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment