This question already has an answer here:

I want that line from the file which have highest length among all the lines using awk command.

marked as duplicate by Sundeep, G-Man, Jesse_b, roaima, Community Apr 29 '18 at 7:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What is your definition of "highest length"? You say you want multiple lines, does that mean like "Top 5 highest length"? – Jesse_b Apr 28 '18 at 14:33
  • I read it as “if there’s a tie, all the longest lines” – Jeff Schaller Apr 28 '18 at 14:34
  • No I only want that "line which has higher length among all the lines" and "if there is tie then all the longest line" – Shah Honey Apr 28 '18 at 15:24
  • "higher" is a comparative word, and as such needs qualification. Do you mean "highest", i.e. the "longest line or joint-longest lines"? – roaima Apr 28 '18 at 15:33
  • 1
    there are other solutions in that question that handle more than one line with max length... in gawk as well as other tools – Sundeep Apr 28 '18 at 16:20
awk '{ if (length($0)>maxlength) { maxlength=length($0); longest_line=$0; } };
     END { print longest_line; }' inputfile

Check length of line (if no arguments passed to the length function it uses $0, the whole line).

Where length is greater than variable x, set x to the length. And set variable a to the contents of the line.

Finally, on reaching end of file, print the contents of variable a.

awk 'length>x{x=length;a=$0}END{print a}' inputfile

Try it online!

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