Longest line in a file

BashShellUtilities

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

Using wc (GNU coreutils) 7.4:

wc -L filename

gives:

101 filename

Solution 2 - Bash

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

For reference : Finding the longest line in a file

Solution 3 - Bash

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

Solution 4 - Bash

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"

Solution 5 - Bash

wc -L < filename

gives

101

Solution 6 - Bash

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

Solution 7 - Bash

Looks all the answer do not give the line number of the longest line. Following command can give the line number and roughly length:

$ cat -n test.txt | awk '{print "longest_line_number: " $1 " length_with_line_number: " length}' | sort -k4 -nr | head -3
longest_line_number: 3 length_with_line_number: 13
longest_line_number: 4 length_with_line_number: 12
longest_line_number: 2 length_with_line_number: 11

Solution 8 - Bash

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

Solution 9 - Bash

Here are references of the anwser

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

http://wtanaka.com/node/7719

Solution 10 - Bash

In perl:

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

this only prints the line, not its length too.

Solution 11 - Bash

I'm in a Unix environment, and work with gzipped files that are a few GBs in size. I tested the following commands using a 2 GB gzipped file with record length of 2052.

  1. zcat <gzipped file> | wc -L

and

  1. zcat <gzipped file> | awk '{print length}' | sort -u

The times were on avarage

  1. 117 seconds

  2. 109 seconds

Here is my script after about 10 runs.

START=$(date +%s) ## time of start

zcat $1 |  wc -L

END=$(date +%s) ## time of end
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

START=$(date +%s) ## time of start

zcat $1 |  awk '{print length}' | sort -u

END=$(date +%s) ## time of end
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

Solution 12 - Bash

Variation on the theme.

This one will show all lines having the length of the longest line found in the file, retaining the order they appear in the source.

FILE=myfile grep `tr -c "\n" "." < $FILE | sort | tail -1` $FILE

So myfile

x
mn
xyz
123
abc

will give

xyz
123
abc

Solution 13 - Bash

Just for fun, here's the Powershell version:

cat filename.txt | sort length | select -last 1

And to just get the length:

(cat filename.txt | sort length | select -last 1).Length

Solution 14 - Bash

If you are using MacOS and are getting this error: wc: illegal option -- L you dont need to install GNU sipmly do this.

If all you want to do is just get the count of the characters in the longest line of the file and you are using OS X run:

awk '{print length}' "$file_name" | sort -rn | head -1

Something like this;

echo "The longest line in the file $file_name has $(awk '{print length}' "$file_name" | sort -rn | head -1) characters"

Outputs:

The longest line in the file my_file has 117 characters

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew ProckView Question on Stackoverflow
Solution 1 - BashDanielView Answer on Stackoverflow
Solution 2 - BashPale Blue DotView Answer on Stackoverflow
Solution 3 - BashRamonView Answer on Stackoverflow
Solution 4 - BashJensView Answer on Stackoverflow
Solution 5 - BashAnonymousView Answer on Stackoverflow
Solution 6 - BashChris KoknatView Answer on Stackoverflow
Solution 7 - BashwangfView Answer on Stackoverflow
Solution 8 - BashJohn KearneyView Answer on Stackoverflow
Solution 9 - BashNadir SOUALEMView Answer on Stackoverflow
Solution 10 - BashrspView Answer on Stackoverflow
Solution 11 - BashJonView Answer on Stackoverflow
Solution 12 - Bashmartin claytonView Answer on Stackoverflow
Solution 13 - BashEddie GrovesView Answer on Stackoverflow
Solution 14 - BashIvansito87View Answer on Stackoverflow