Harness the Combinatoric Power of Command-Line Tools and Utilities

../Tips

Find Long Lines With awk

Commands files

Published December 13, 2025 and last verified on December 13, 2025

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.js

You’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:
    • NR is awk’s built-in variable for the current line number
    • length shows 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.py

To see the actual content of long lines, include the line text by using #0:

awk 'length > 80 {print NR": "length" chars - "$0}' filename.js

This is useful for identifying lines that you’ll need to reformat to meet coding standards or readability guidelines.