Harness the Combinatoric Power of Command-Line Tools and Utilities
Find Long Lines With awk
Published December 13, 2025 and last verified on December 13, 2025
❗ This article is more than six months old. Some things may not work as written.
When working with code or text files, you may need to find lines that exceed a certain character limit, especially when following coding standards that enforce line length limits.
You can use awk to find lines longer than a specific number of characters. For example, the following command shows you the line numbers and character counts for any lines longer than 80 characters in the file app.js:
awk 'length > 80 {print NR": "length" chars"}' app.jsYou’ll receive output that looks like the following:
15: 85 chars
23: 92 chars
47: 106 chars
Here’s how it works:
length:awk’s built-in function that returns the length of the current line> 80: The condition that checks if the line length exceeds 80 characters{print NR": "length" chars"}: The action to perform when the condition is true:NRisawk’s built-in variable for the current line numberlengthshows the actual character count- The output format shows both line number and character count
You can adjust the character limit by changing the number:
awk 'length > 120 {print NR": "length" chars"}' filename.pyTo see the actual content of long lines, include the line text by using #0:
awk 'length > 80 {print NR": "length" chars - "$0}' filename.jsThis is useful for identifying lines that you’ll need to reformat to meet coding standards or readability guidelines.