Tip #741 Find the longest line in a file
This is how we can find the length of the longest line in a file.awk ' { if ( length > L ) { L=length} }END{ print L}' file.txt
Sample output:
36
We can also print the longest line along with the length:
awk ' { if ( length > L ) { L=length ;s=$0 } }END{ print L,"\""s"\"" }' file.txt
Sample output:
36 "longest line in the file is this one"
- TAGS:
- awk
alias aptitude at awk bash bc cal cat cd colrm comm cp csh curl cut date dd df dialog diff dirname dpkg du fc find fuser grep gs gzip history iconv kill ksh last less ln ls lsof lynx m4 md5sum mkdir mkfifo mkisofs mv mysql nc netstat openssl OSX perl ping popd ps pushd python read redirection rm scp screen sed sort ssh stat sudo svn tail tar tee test top tr uniq vim wc wget xargs