Longest line in a file

前端 未结 14 2122
鱼传尺愫
鱼传尺愫 2020-11-27 10:34

I\'m looking for a simple way to find the length of the longest line in a file. Ideally, it would be a simple bash shell command instead of a script.

14条回答
  •  不知归路
    2020-11-27 11:24

    perl -ne 'print length()."  line $.  $_"' myfile | sort -nr | head -n 1
    

    Prints the length, line number, and contents of the longest line

    perl -ne 'print length()."  line $.  $_"' myfile | sort -n
    

    Prints a sorted list of all lines, with line numbers and lengths

    . is the concatenation operator - it is used here after length()
    $. is the current line number
    $_ is the current line

提交回复
热议问题