Home Linux Commandline Tips How to Find Longest Line(s) in a File in Linux

How to Find Longest Line(s) in a File in Linux

The Linux operating system likes to brag about its computing power and prowess. Its algorithmic approach to things like file processing especially under file management yields important milestones for Linux users on the quest to mastering Linux administration footprints.

One aspect of file processing under the Linux operating system environment that we must greatly consider is identifying the longest lines within an editable Linux-supported file.

Practical Implication of Long Lines in a File

Consider the scenario where you work at a company or you are dealing with a project that processes huge log files. These files might be rendered as single text lines when in reality they can be encapsulating thousands of JSON documents.

If the size of these text lines is very/unusually long, processing them via a proxy server might be required to correctly redirect the file(s) to a destination server like an elastic search server.

However, such careful steps to file processing might lead to unintended file processing errors when in reality you are just dealing with extra long lines in your files. Diagnosing such an error is impossible without knowing the menace in play.

This tutorial will take through the steps needed to identify the longest lines within a targeted file on a Linux operating system environment.

Problem Statement

To make this article more fun and engaging, we are going to create a reference text file with several varying lines in it and later implement valid Linux solutions to find out the longest lines.

$ sudo nano sample_file.txt
Linux File with Longest Lines
Linux File with Longest Lines

We will be striving to identify the longest lines on the above file (sample_file.txt) via useful Linux commands.

1. Find Longest Line in a File Using Awk Command

Ideally, we could prepend all lines in the above file using a one-liner awk command to determine their exact lengths as demonstrated below.

$ awk '{printf "%2d| %s\n",length,$0}' sample_file.txt  
Find Longest Line in File
Find Longest Line in the File

As per the screen capture above, 73 is the largest line length.

Print Longest Line in a File Using Using wc and grep Commands

By combining these two commands, you get to use regex from the grep command and max-line-length from wc command. The wc command takes the -L command option to determine max-line-length as demonstrated below.

$ grep -E "^.{$(tr '\t' ' '

The above command should print the longest lines on the file sample_file.txt.

Print Longest Line in File
Print Longest Line in File

Since we had two identical lines with the largest line length of 73, the above command printed the two lines. If it was only one line with the largest line length of 73, only that line would be printed.

We are now comfortable in finding the longest line(s) in a file in Linux.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.