Skip to content

Instantly share code, notes, and snippets.

@rohitprajapati
Created February 19, 2016 08:34
Show Gist options
  • Save rohitprajapati/6d8535705898a082e259 to your computer and use it in GitHub Desktop.
Save rohitprajapati/6d8535705898a082e259 to your computer and use it in GitHub Desktop.
Given a csv file with header. Find the max size(character length) required for each column.
# Usage: awk -f max_column_size.awk ~/Desktop/test.csv
# csv header required
BEGIN {
FS=","
OFS=","
}
NR == 1 {
for(n = 1; n <= NF; n++) {
colname[n]=$n
}
}
NR > 1 {
for(n = 1; n <= NF; n++) {
if (length($n)>maxlen[n])
maxlen[n]=length($n)
}
}
END {
for(i = 1; i <= NF; i++) {
print colname[i], ":", maxlen[i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment