Claude defaults to wc -l for counting lines, producing off-by-one errors on files without trailing newlines

Resolved 💬 3 comments Opened Mar 27, 2026 by godfrey-altmetric Closed Apr 29, 2026

Description

When asked to count lines in a file, Claude consistently uses wc -l. This command counts newline characters, not lines — per POSIX spec, a "line" is a string terminated by \n. Files that don't end with a trailing newline (common with CSVs exported from Google Sheets, Excel, or many editors) will be undercounted by 1.

Steps to reproduce

  1. Create a file without a trailing newline:

``
printf "header\nrow1\nrow2" > test.csv
``

  1. Ask Claude: "how many lines are in test.csv?"
  2. Claude runs wc -l test.csv and reports 2 lines instead of 3

Expected behavior

Claude should use a line-counting method that handles files with or without trailing newlines, such as:

  • grep -c '' file
  • awk 'END{print NR}' file

Why this matters

  • The error is silent — there's no warning that the count might be off
  • Users who aren't developers won't know about this POSIX quirk
  • It affects all users, not just one session
  • Off-by-one errors on data files (CSVs, logs) can cascade into wrong assumptions about data completeness

Suggested fix

Update Claude's system instructions or tool guidance to prefer grep -c '' over wc -l when counting lines, similar to how cat is already discouraged in favor of the Read tool.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗