I was attempting to import a bunch of records into a database (~9.1 GB), but it turns out that I didn’t make the fields the right length. I found values that exceeded the database column’s length on line 1,220, then 1,590, then 7,460 and finally 28,000-odd. I had to expand a database column (not necessarily the some one) by a few characters each time.
So rather than just keep going and then find that around line four zillion, something else was too small, I’d find out the maximum field length per field. A quick bit of searching found this stackoverflow article: http://stackoverflow.com/questions/8629973/unix-maxlength-of-each-column-in-file.
Code is reproduced here for convenience, all thanks due to Mat for the solution and toop for asking the question.
$ cat t.awk
#!/bin/awk -f
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 in maxlen) {
print colname[i], ":", maxlen[i];
}
}
$ awk -F',' -f t.awk data-noquotes.dat
(I changed the delimiter and input file name to suit my circumstances.)
March 19, 2014 at 10:11 pm
Great script.
My version below outputs the fields in the order they appear.
#!/bin/awk -f
NR==1{
for(n = 1; n 1{
for(n = 1; n maxlen[n])
maxlen[n]=length($n)
}
}
END {
printf(“\n==> %s <==\n", FILENAME)
for (i in maxlen) {
k++
printf("%s:%s\n", colname[k] , maxlen[k]);
}
}