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
Output:
25
And also to print the longest line along with the length:
$ awk ' { if ( length > L ) { L=length ;s=$0 } }END{ print L,"\""s"\"" }' file.txt
Output:
25 "136068 sumAQSD.1236800000"
Read about awk built in string manipulation function length
4 comments:
Output the length of the longest line in a file:
$ wc -L file.txt
@kg23, thanks a lot for this. really helpful. never knew about this. thanks
-L, --max-line-length
print the length of the longest line
Note that some (if not many) implementations do not include the "-L" option for wc.
if='file.txt'
< $if sed 'y/]/./;s/.*/[&]/' |
dc -e "
#####
## get longest line length
#####
#####
# macros
####
[q]sq
[sA]sM
[?z0=qZdlA max length
# register 'l' => line.
?dZsmsl
# computations
lbx
###
## display result
##
# push the fields onto the main stack in reverse
# order from which they need to be printed.
[$QQ$NL]ll[$SP$QQ]lm
# now print the stack
lpx
"
Post a Comment