Longest line in a file

前端 未结 14 1978
鱼传尺愫
鱼传尺愫 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:29

    In perl:

    perl -ne 'print ($l = $_) if (length > length($l));' filename | tail -1
    

    this only prints the line, not its length too.

    0 讨论(0)
  • 2020-11-27 11:31

    Important overlooked point in the above examples.

    The following 2 examples count expanded tabs

      wc -L  <"${SourceFile}" 
    # or
      expand --tabs=8 "${SourceFile}" | awk '{ if (length($0) > max) {max = length($0)} } END { print max }'
    

    The following 2 count non expaned tabs.

      expand --tabs=1 "${SourceFile}" | wc -L 
    # or
      awk '{ if (length($0) > max) {max = length($0)} } END { print max }' "${SourceFile}"
    

    so

                  Expanded    nonexpanded
    $'nn\tnn'       10            5
    
    0 讨论(0)
提交回复
热议问题