Longest line in a file

前端 未结 14 2085
鱼传尺愫
鱼传尺愫 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条回答
  •  萌比男神i
    2020-11-27 11:07

    Just for fun and educational purpose, the pure POSIX shell solution, without useless use of cat and no forking to external commands. Takes filename as first argument:

    #!/bin/sh
    
    MAX=0 IFS=
    while read -r line; do
      if [ ${#line} -gt $MAX ]; then MAX=${#line}; fi
    done < "$1"
    printf "$MAX\n"
    

提交回复
热议问题