Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for the simplest method to print the longest line in a file. I did some googling and surprisingly couldn't seem to find an answer. I frequently print the length of the longest line in a file, but I don't know how to actually print the longest line. Can anyone provide a solution to print the longest line in a file? Thanks in advance.

share|improve this question
What about when there are multiple "longest" lines?. Because you want more than a simple maximum length, do you want to see all instances of lines which are equal longest? – Peter.O Nov 13 '11 at 14:20
up vote 19 down vote accepted
cat ./text | awk ' { if ( length > x ) { x = length; y = $0 } }END{ print y }'

UPD: summarizing all the advices in the comments

awk 'length > max_length { max_length = length; longest_line = $0 } END { print longest_line }' ./text 
share|improve this answer
Useless Use of cat. awk '{ if ( length > x ) { x = length; y = $0 } }END{ print y }' text – Keith Thompson Nov 13 '11 at 1:37
It is, both calling another command (cat), and using a pipe are expensive operations, not to mention that it's more efficient for awk to just read the file. The performance implications are definitely noticeable if this is done frequently, and even so, you are completely misusing cat. – Chris Down Nov 13 '11 at 2:37
The slightly shorter and more idiomatic: awk 'length > x { x = length; y = $0 } END { print y }'. Nothing wrong with your answer, dimitry, just presenting an alternative to help people understand awk a little better. – camh Nov 13 '11 at 5:10
@dmitry.malikov Do what I do; every time somebody points out a UUOC in my answer I add another one on. cat ./text | cat - | awk ' { if ( length > x ) { x = length; y = $0 } }END{ print y }' – Michael Mrozek Nov 13 '11 at 5:41
@Michael Mrozek - there's a reason it's considered useless, and your reply is childish and representative of your stubbornness to listen to reason. There is no reason to use cat with awk, grep, sed, etc., since they can all natively handle files directly. – laebshade Nov 13 '11 at 16:22
sed -rn "/.{$(<file expand -t1 |wc -L)}/{p;q}" file

This first reads the file inside the command substitution and outputs the length of the longest line, (previously, expand converts tabs to spaces, to overcome the semantics of wc -L -- each tab in the line will add 8 instead of 1 to line length). This length is then used in a sed expression meaning "find a line this number of characters long, print it, then quit". So this actually can be as optimal as the longest line is near to the top of the file, heheh (thanks fered for the awesome and constructive comments).

Another, I had thought earlier than the sed one (in bash):

while read -r line; do
    (( ${#line} > max )) && max=${#line} && longest="$line"
echo "$longest"
share|improve this answer
This method is very expensive and slow. – Chris Down Nov 13 '11 at 2:47
@Chris Down: Oh yes it is. But the question was about the sortest method, not the most efficient. WOrks finely for small to medium files or non critical tasks, though. – ata Nov 13 '11 at 11:49
WARNING: wc's option -L, --max-line-length prints the length of the longest line, according to the man page, but if you dig deeper (as in when you get wrong / unexpected results), you find that this option increments the length by 8 for each 1 tab char \x09 see this Unix & Linux Q/A – Peter.O Nov 13 '11 at 13:05
PS. Your answer will print all the "equally longest" lines, which is probably a good thing... To force wc to count only 1 char per tab, this works. sed -rn "/.{$(<file expand -t1 |wc -L)}/p" file – Peter.O Nov 13 '11 at 14:13
read line will interpret backslash-escaped chars as the literal char, eg \A resloves to A, which of course effectively reports a shorter than actual byte-usage... To prevent this escaped interpretation, use: read -r line . . . . Also, to make the sed+wc version quit after the first "longest line", change p to {p;q} .. sed -rn "/.{$(<file expand -t1 |wc -L)}/{p;q}" file – Peter.O Nov 14 '11 at 3:15

In pure bash:


while IFS= read -r _line; do
    if (( _length > _max_length )); then
        _max_line=( "${_line}" )
    elif (( _length == _max_length )); then
        _max_line+=( "${_line}" )

printf 'Max line length: %d\n' "${_max_length}"
printf 'Lines matched with that length: %d\n' "${#_max_line[@]}"
(( ${#_max_line[@]} )) && printf '%s\n' '----------------' "${_max_line[@]}"
share|improve this answer
As-is, the code can return invalid results. Setting _max_line[0]=${_line} does not remove the rest of any previously accumulated shorter "longest lines"... unset _max_line will clear the entire array... – Peter.O Nov 13 '11 at 21:00
@fered Thanks for that, was written pretty quickly. Fixed. – Chris Down Nov 13 '11 at 21:03

The following example was going to be, and should have been, a comment to dmitry.malikov's answer, but because of the Useless Use of Visible Comment Space there, I've chosen to present it here, where it will at least be seen...

This is a simple variation of the dmitry's single-pass awk method.
It prints all "equal longest" lines. (Note. delete array is a gawk extension).

awk 'length >x { delete y; x=length }
     length==x { y[NR]=$0 } END{ for (z in y) print y[z] }' file
share|improve this answer

Here's a Perl solution:

perl -e 'while(<>){
           $l>$m && do {$c=$_; $m=$l}  
         } print $c' file.txt 

Or, if you want to print all the longest lines

perl -e 'while(<>){
           push @{$k{$l}},$_;
           $m=$l if $l>$m;
         } print @{$k{$m}}' file.txt 

Since I had nothing better to do, I ran some benchmarks on a 625M text file. Surprisingly, my Perl solution was consistently faster than the others. Granted, the difference with the accepted awk solution is tiny, but it is there. Obviously, solutions that print multiple lines are slower so I have sorted by type, fastest to slowest.

Print only one of the longest lines:

$ time perl -e 'while(<>){
           $l>$m && do {$c=$_; $m=$l}  
         } print $c' file.txt 
real    0m3.837s
user    0m3.724s
sys     0m0.096s

$ time awk 'length > max_length { max_length = length; longest_line = $0 }
 END { print longest_line }' file.txt
real    0m5.835s
user    0m5.604s
sys     0m0.204s

$ time sed -rn "/.{$(<file.txt expand -t1 |wc -L)}/{p;q}" file.txt 
real    2m37.348s
user    2m39.990s
sys     0m1.868s

Print all longest lines :

$ time perl -e 'while(<>){
           push @{$k{$l}},$_;
           $m=$l if $l>$m;
         } print @{$k{$m}}' file.txt 
real    0m9.263s
user    0m8.417s
sys     0m0.760s

$ time awk 'length >x { delete y; x=length }
     length==x { y[NR]=$0 } END{ for (z in y) print y[z] }' file.txt
real    0m10.220s
user    0m9.925s
sys     0m0.252s

## This is Chris Down's bash solution
$ time ./a.sh < file.txt 
Max line length: 254
Lines matched with that length: 2
real    8m36.975s
user    8m17.495s
sys     0m17.153s
share|improve this answer

Grep the first longest line

grep -Em1 "^.{$(wc -L <file.txt)}\$" file.txt 

The command is unusually hard to read without practise because it mixes shell- and regexp syntax.
For explanation, I will use simplified pseudocode first. The lines starting with ## do not run in the shell.
This simplified code uses the file name F, and leaves out quoting and parts of regexps for readability.

How it works

The command has two parts, a grep- and a wc invocation:

## grep "^.{$( wc -L F )}$" F

The wc is used in a process expansion, $( ... ), so it is run before grep. It calculates the length of the longest line. The shell expansion syntax is mixed with the regular expression pattern syntax in a confusing way, so I will decompose the process expansion:

## wc -L F
## grep "^.{42}$" F

Here, the process expansion was replaced with the value it would return, creating the grep commandline that is used. We can now read the regular expression more easily: It matches exactly from start (^) to end ($) of the line. The expression between them matches any character except newline, repeated by 42 times. Combined, that is lines that consist of exactly 42 characters.

Now, back to real shell commands: The grep option -E (--extended-regexp) allows to not escape the {} for readability. Option -m 1 (--max-count=1) makes it stop after the first line is found. The < in the wc command writes the file to its stdin, to prevent wc from printing the file name together with the length.

Which longest lines?

To make the examples more readable with the filename occurring twice, I will use a variable f for the filename; Each $f in the example could be replaced by the file name.


Show the first longest line - the first line that is as long as the longest line:

grep -E -m1 "^.{$(wc -L <"$f")}\$" "$f"

Show all longest lines - all lines that are as long as the longest line:

grep -E "^.{$(wc -L <"$f")}\$" "$f" 

Show the last longest line - the last line that is as long as the longest line:

tac "$f" | grep -E -m1 "^.{$(wc -L <"$f")}\$"

Show the single longest line - the longest line longer than all other lines, or fail:

[ $(grep -E "^.{$(wc -L <"$f")}\$" "$f" | wc -l) = 1 ] && grep -E "^.{$(wc -L <"$f")}\$" "$f" 

(The last command is even more inefficient than the others, as it repeats the complete grep command. It should obviously be decomposed so that the output of wc and the lines written by grep are saved to variables.
Note that all longest lines may actually be all lines. For saving in a variable, only the first two lines need to be kept.)

share|improve this answer
cat filename | awk '{ print length }' | sort -n | tail -1
share|improve this answer

You can use wc:

wc -L fileName
share|improve this answer
Please read the question again. The required output is the longest line itself, not the length of the longest line. Also see Peter.O's comment regarding wc -L's drawback. – manatwork May 1 '13 at 11:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.