Command line

Numbering a Million Lines, Measured: cat -n vs nl vs awk vs grep -n

The fastest tool numbers every line; the tool built for the job numbers only 950,000 of them by default and stops numbering entirely after one stray line; and the flag that makes nl match cat -n exactly costs 2.8 times the speed for nothing in the output.

Written November 10, 2026 · 11 min read

By the end of this guide
  • The table: the fastest tool is also the most correct
  • The closest published work asserts without a table
  • The divergence starts at line 21 and never heals
  • The line that eats all your numbers

What this guide is

One corpus, six arms, three reps each. The corpus is 1,000,000 lines, 74,150,000 bytes, with every 20th line blank, 50,000 blanks in total, 5 percent of the file. Every arm prints the numbered lines to a file, and every rep is checked the same way: line count, numbered-line count, last number, and a sha256 of the whole output, so a timing number can never come from a run that printed something different. The question is the one people ask before numbering a log for a colleague or a diff for a review: which tool numbers it fastest, and which tools number it wrong in ways the clock does not show.

The answer is a correction in both directions. The fastest tool is also the most correct one here: cat -n numbers every line at 0.0768 seconds, about 966 MB/s of input. The tool purpose-built for numbering, nl, is 2.76 times slower and numbers only 950,000 of the 1,000,000 lines by default, silently dropping the count after the first blank line. And nl -ba, the flag that makes nl number every line, produces output byte-identical to cat -n, the same sha256 on all three reps each, at 2.76 times the cost, so there is no output reason to pay for it. The two traps decide more real numbering jobs than the speed table does: the blank-line divergence starts at line 21 and never heals, and a lone backslash-colon line un-numbers the rest of the file.

The table: the fastest tool is also the most correct

Wall seconds, medians of three runs, fresh tonight. Every rep returned rc=0, every rep printed exactly 1,000,000 lines, and every arm's output sha256 was identical across its three reps.

  • cat -n: fresh median 0.0768, reps 0.1020, 0.0768, 0.0729. Night before: 0.0746, reps 0.1028, 0.0746, 0.0743. Numbers 1,000,000 lines, last number 1,000,000.
  • cat -b: fresh median 0.0716, reps 0.0764, 0.0714, 0.0716. Night before: 0.0708, reps 0.0706 to 0.0721. Numbers 950,000 lines, last number 950,000.
  • nl (default): fresh median 0.2123, reps 0.2123, 0.2140, 0.2113. Night before: 0.2100, reps 0.2099 to 0.2341. Numbers 950,000 lines, last number 950,000.
  • nl -ba: fresh median 0.2119, reps 0.2103, 0.2187, 0.2119. Night before: 0.2158, reps 0.2135 to 0.2165. Numbers 1,000,000 lines, last number 1,000,000.
  • awk '{print NR, $0}': fresh median 0.3009, reps 0.3000, 0.3038, 0.3009. Night before: 0.3098, reps 0.3068 to 0.3102. Numbers 1,000,000 lines, last number 1,000,000.
  • grep -n '': fresh median 0.1462, reps 0.1462, 0.1548, 0.1448. Night before: 0.1448, reps 0.1444 to 0.1509. Numbers 1,000,000 lines, last number 1,000,000.

Against cat -n at 0.0768 seconds tonight, cat -b ties it at 0.93 times, skipping the number on blank lines costs nothing measurable. grep -n costs 1.90 times (1.94 the night before), nl 2.76 times (2.82 the night before), nl -ba 2.76 times (2.89 the night before), and awk 3.92 times (4.15 the night before). The ranking is identical on both nights and every ratio moved by less than a tenth, so the ordering is stable and the exact ratios are one machine's.

Two cells are noisy and the guide says so rather than hiding it. The cat -n first rep is the cold spike both nights: 0.1020 seconds tonight and 0.1028 the night before against medians near 0.073 to 0.077. A one-run timing of cat -n would report about a third more than the steady cost. The nl third rep the night before was 0.2341 against 0.2099 to 0.2100 on the other two, and grep -n tonight spans 0.1448 to 0.1548. The cells sit near the spawn-and-read floor at 0.07 to 0.31 seconds, so treat the cat -n against cat -b tie as order-of-magnitude; the nl and awk gaps, 2.8 times and 3.9 times, are the stable claims.

The closest published work asserts without a table

The closest published work is a line-counting guide, not a line-numbering guide, and the distinction is the gap this probe fills. The CopyProgramming shell line-counting piece asserts that cat -n is the fastest for simple file line numbering, with no timing table, no corpus, no reps, and no output verification. Its comparison table covers counting methods, wc -l against grep -c against awk END NR against sed -n $=, which print one number, not a million numbered lines. The LinuxHaxor counting guide times the same counting job, wc -l against awk END NR against sed -n $=, on a 1 GB file, and the unix.com thread times counting on a 2 GB file the same way. None of them prints numbered output, and none of them runs nl at all.

The prose explainers cover the blank-line difference without measuring it. Several pieces explain that nl skips blank lines by default while cat -n numbers them, and that nl -ba numbers all lines, but they give no timings, no corpus, and no output comparison. Counting lines and printing numbered lines are different jobs, and the counting tables do not answer the numbering question.

The divergence starts at line 21 and never heals

The first blank line is line 20, and that is where the two numberings part. On line 21 cat -n says 21 and nl says 20, because nl skipped the blank line 20 while cat -n numbered it. From there the two numberings never agree again: every later line is off by the blank count so far, and the final numbers are 1,000,000 against 950,000, a gap of exactly the 50,000 blank lines in the corpus. The probe verified the divergence line tonight and the night before, identical both times.

The manual states the default directly: default options are -bt, body numbering style t, number only nonempty lines, with -ba numbering all lines. cat -n numbers all output lines, and cat -b numbers nonempty output lines and overrides -n, which is why cat -b and nl agree on the count, 950,000 numbered lines each, while cat -n and nl -ba agree on the full 1,000,000. Anyone diffing nl output against cat -n output, or using nl numbers as line references back into the file, is off by the blank count from the first blank onward. On a file with no blanks the two agree; on a file that is mostly blanks the gap widens past the 5 percent measured here. The fix is nl -ba if the nl format is wanted, or cat -n if the number is the point.

The line that eats all your numbers

A lone backslash-colon line in the file is nl's logical-page footer delimiter, and everything after it falls in the footer section, which is unnumbered by default. The probe ran it on a seven-line file, alpha beta gamma, then a line holding exactly backslash-colon, then delta epsilon zeta. cat -n numbers 1 through 7. nl numbers 1, 2, 3 and then nothing: delta, epsilon, and zeta come out unnumbered, and the delimiter line itself prints as a blank line. The numbers tonight are identical to the night before, and the trap file is the same on both nights.

nl -p does not fix it, and the probe verified that rather than assuming it. The -p flag only stops page-break resets; this is a section change, not a page break, and the manual lists the defaults that make it so: footer numbering style n, number no lines, with the delimiter characters backslash-colon. The fix is to strip the line first, or to use cat -n or awk, which have no section concept.

The byte-identical pair

nl -ba output and cat -n output have the same sha256 on all three reps each, tonight and the night before. Same numbers, same separator, same lines, byte for byte. The two commands take different paths to the same file: nl -ba at a fresh median of 0.2119 seconds, cat -n at 0.0768. Paying 2.76 times buys nothing in the output.

That is the one place where the choice is not a trade-off at all. cat -b against nl is a real choice: both number 950,000 lines, both skip the blanks, but cat -b does it at 0.0716 seconds against nl at 0.2123, and cat -b has no section delimiters to trip over. grep -n against awk is a real choice too: grep -n is faster at 0.1462 against 0.3009, but its separator is a colon, 21:line, where awk prints a space, 21 line, and cat -n and nl print a tab. A downstream parser that splits on the separator has to know which one it is getting, and the probe's output hashes record the difference: the four full-numbering arms do not share one output, only the cat -n and nl -ba pair does.

Checklist

  • Before numbering a file, check the blank rate: on a file with 5 percent blanks nl numbers 950,000 of 1,000,000 lines, and the divergence starts at line 21, the line after the first blank.
  • Before paying for nl -ba, check the output: it is byte-identical to cat -n, the same sha256 on all three reps each, at 0.2119 seconds against 0.0768, so there is no output reason to pay 2.76 times.
  • Before trusting nl on an unknown file, check for a lone backslash-colon line: it is the footer delimiter, and everything after it comes out unnumbered with a zero exit code, and nl -p does not fix it.
  • Before quoting a ratio, check the range: cat -n first rep is 0.1020 seconds against a 0.0768 median on both nights, so single runs lie.
  • Before picking between grep -n and awk, check the separator: grep -n prints 21:line with a colon at 0.1462 seconds, awk prints 21 line with a space at 0.3009, and cat -n and nl print a tab.
  • Before reusing these numbers, check the setup: WSL2, 32 CPUs, cat and nl 9.4, awk 5.2.1, grep 3.11, tmpfs, warm page cache, one thread per arm, one 1,000,000-line corpus with 5 percent blanks, sha256 recorded in the probe log.

Sources

  • Linux man-pages, cat(1) — read 2026-11-10: -n numbers all output lines, -b numbers nonempty output lines and overrides -n. Box runs GNU coreutils 9.4; the manual text is coreutils 9.11 and every cited behavior was exercised by the probe itself.
  • Linux man-pages, nl(1) — read 2026-11-10: default options are -bt -d'\:' -fn -hn, body style t numbers only nonempty lines, style a numbers all lines, footer style n numbers no lines, and -p does not reset line numbers for each section. Box runs GNU coreutils 9.4; the manual text is coreutils 9.11 and every cited behavior was exercised by the probe itself.
  • CopyProgramming, How to Count Lines in a File Using Shell Commands — read 2026-11-10: asserts cat -n is the fastest for simple file line numbering, with no timing table, no corpus, no reps, and no output verification; its comparison table covers counting methods that print one number, not a million numbered lines. The closest published work, and the gap this probe fills.
  • LinuxHaxor, How to Count the Number of Lines in a File in Bash — read 2026-11-10: times the counting job, wc -l against awk END NR against sed -n $=, on a 1 GB file. It counts lines; it does not print numbered output, and it does not run nl.
  • Veldica, Cutting a Million-Line File Into Ten: split vs csplit vs awk, Measured — the sibling guide: measures cutting a million-line file into ten chunks on the same corpus scale and the same harness lineage. This guide numbers the lines; it does not cut them.
  • Veldica, comm vs grep vs sort|uniq vs Python: Set Intersection, Measured — the sibling guide: measures set intersection on a million lines, a different operation on the same scale. This guide prints numbered output; it does not intersect sets.

Keep Exploring