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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find the longest record in a file 1

Status
Not open for further replies.

mwesticle

Programmer
Nov 19, 2003
51
US
I need to run something ('awk', 'sed', etc.) that will let me know what the longest record in a file is. And I need it to ignore spaces. So, if I have a file with records that look like this (without the ticks):

'John Doe'
'Al Doe'
'Christopher Doe'
'Jill Doe'

I want the returned result to somehow let me know that the longest length record in the file is 15 bytes ('Christopher Doe'). I don't even care which record it is, I just need the longesth length. Anyone know how to do this?
 
nawk '
{ if (length($0) > max) {
max=length($0);
str= $0
}
} END{ printf("max->[%d] str->[%s]\n", max, str)}' file.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top