Longest line in a file

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"

awk '{print length, $0}' Input_file |sort -nr|head -1

For reference : Finding the longest line in a file


Using wc (GNU coreutils) 7.4:

wc -L filename

gives:

101 filename

awk '{ if (length($0) > max) {max = length($0); maxline = $0} } END { print maxline }'  YOURFILE