Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

longest string by field 1

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
I have a problem that is kind of elusive::I have been trying to solve it in various ways but none seem to work 100%.

I have a tab delimited flat file with n fields, and any number of records.
I need the longest string length for each field and I need a clean summary, by field, of the longest string in that field printed to stdout.
I'm no novice to awk anymore and can do this easily by field individually, but in aggregate it poses a problem.
Does anyone have a quick solution for this?

Thanks
 
BEGIN {
FS="\t"
}

{
for(i=1; i <= NF; i++)
if (length($i) > length(arr))
arr=$i
}

END {
for (i in arr)
printf(&quot;field->[%d] LongestValue->[%s]\n&quot;, i, arr);
}

vlad
 
Geez that was easy, I made it way too complicated.
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top