BLOG / GUIDE · 2026-04-18 · 7 min

How to count lines in a text file or string

Six ways to count lines, words, and characters — from wc on the command line to Excel COUNTA, the Word Count dialog, and an instant browser tool. With the trailing-newline gotcha that fools every basic tool.

The short answer. Paste your text into the line counter and read five numbers at once: total lines, non-blank, blank, words, characters. In a terminal, wc -l file.txt. In Notepad++, the bottom status bar shows the line count of the file or the selection. In VS Code, the same number is in the status bar at the bottom-right (Ln X, Col Y). In Excel, =COUNTA(A:A) gives non-blank cell count; =ROWS(A1:A100) gives the range length. In Word, the Review tab's Word Count dialog includes lines.

What's a line, actually?

"Count the lines" sounds obvious, but three definitions are quietly in play and they don't always agree:

  • Newline-terminated lines — what wc -l counts: the number of \n characters. A file with five lines but no trailing newline reports four.
  • Logical lines / paragraphs — what most text editors show: count of line endings including the last unterminated line. A five-line file with no final newline reports five.
  • Non-blank lines — what most humans mean when they say "how many items are in the list?". Blank lines are dropped first.

The difference between wc -l and an editor's status bar is the most common source of "the tool says one thing, my eyes say another" confusion. Both are right; they're answering different questions.

How do you count lines in a browser, instantly?

Open the line counter. Paste any text. Five numbers update live:

  • TOTAL LINES — logical line count (the editor-style version, including the last line whether or not it ends with a newline).
  • NON-BLANK — lines that contain at least one non-whitespace character.
  • BLANK — lines that are empty or whitespace-only.
  • WORDS — runs of non-whitespace separated by whitespace. Punctuation sticks to the adjacent word.
  • CHARACTERS — raw input length, including line breaks. Most emoji count as 2 because JavaScript strings are UTF-16; we may add a grapheme-cluster count later.

Nothing uploads. The whole thing is a few String.split() calls in the page — verifiable in DevTools → Network, where you'll see zero outbound requests after page load.

How do you count lines on the command line?

The Unix one-word answer is wc (word count). For lines:

wc -l file.txt

Useful relatives:

  • wc -l file.txt — line count.
  • wc -w file.txt — word count.
  • wc -m file.txt — character count (locale-aware).
  • wc -c file.txt — byte count (no locale awareness).
  • wc -l *.txt — line counts for every matching file, plus a total at the bottom.

The trailing-newline gotcha. wc -l counts \n characters, not lines. If your file has five lines but the last one doesn't end in \n, the answer is four. To fix, use grep:

grep -c "" file.txt

That returns 5, every time, because grep treats the file as a sequence of lines whether or not the final one is newline-terminated.

To count non-blank lines only:

grep -cve '^[[:space:]]*$' file.txt

Slightly cryptic but exact: the regex matches "a line of nothing but whitespace", and -v inverts it. On Windows, PowerShell's Measure-Object covers the same ground:

(Get-Content file.txt | Measure-Object -Line).Lines

How do you count lines in Notepad++ or VS Code?

Both editors show a live count at the bottom of the window. The exact wording is different.

Notepad++

  • The status bar reads length : NNN   lines : MMM.
  • If you select text, the same status bar shows the selection's length and line count instead.
  • For per-line statistics (longest line, average word count), use View → Summary.

VS Code

  • The bottom-right shows Ln X, Col Y — X is the cursor's line number, indirectly telling you the file size when the cursor is on the last line.
  • Select all (Ctrl+A) and look at the status bar: it shows (X selected) with the character count, and the cursor position confirms the line count.
  • The Command Palette has Go to Line/Column (Ctrl+G) that opens with the total line count in the placeholder.

How do you count lines in Excel?

"Lines" in Excel usually means "rows that have data". Three formulas cover the common cases.

Non-blank cell count.

=COUNTA(A:A)

Returns the number of cells in column A that are not empty. Includes headers; subtract 1 if you want the data count only.

Total rows in a known range.

=ROWS(A1:A100)

Returns 100, regardless of how many of those cells have content.

Count of distinct entries.

=SUMPRODUCT(1/COUNTIF(A2:A100, A2:A100))

Slightly esoteric, but stable across Excel versions. On Excel 365, the cleaner =COUNTA(UNIQUE(A2:A100)) works.

For lines inside a single cell (when a cell contains line breaks created by Alt+Enter):

=LEN(A1) - LEN(SUBSTITUTE(A1, CHAR(10), "")) + 1

Counts \n characters in the cell, plus one for the last line that doesn't end in a newline.

How do you count lines in Microsoft Word?

Word has a dedicated Word Count dialog that includes lines.

  1. Click the Review tab.
  2. In the Proofing group, click Word Count.
  3. The dialog shows pages, words, characters (with and without spaces), paragraphs, and lines.

The Word "line" count is a rendered-page count — it depends on font size, page width, and the current zoom level. A document with 100 paragraphs may show 300 lines if every paragraph wraps to three rendered lines. This is usually what authors and editors want; it is rarely what programmers or data folks want.

For paragraph-style counts (one entry per paragraph mark), use the Paragraphs row from the same dialog — it gives the count of ^p markers, which is closer to what a text editor would call "lines".

Which counting method should you use?

What you want to countBest method
Items in a pasted list (lines, words, chars at once)Browser tool
Lines in a file on diskwc -l — or grep -c "" if you need exact
Rows of data in an Excel column=COUNTA(A:A)
Rendered lines in a Word documentReview → Word Count → Lines
Paragraphs in a Word documentReview → Word Count → Paragraphs
Line count inside an editor while writingNotepad++ or VS Code status bar
Multiple files at oncewc -l *.txt
Non-blank lines onlyBrowser tool's NON-BLANK row, or grep -cv '^[[:space:]]*$'

Frequently asked questions

Why does wc -l give a different number than the editor?

wc -l counts \n characters. If your file's last line doesn't end with a newline, it's not counted — you get one fewer than the editor shows. To get the human-intuitive answer every time, use grep -c "" instead.

How do I count words in a chunk of text?

Three good options. (1) Paste it into the browser tool; the WORDS row updates live. (2) wc -w file.txt on the command line. (3) In Word, the bottom status bar shows a live word count whenever text is selected.

Does the character count include line breaks?

Depends on the tool. The browser tool includes them — the count is the raw string length, so each \n adds one. Word's "Characters (with spaces)" usually does not include paragraph marks. wc -m on the command line includes everything. If precise byte-level counts matter (e.g. for an upload size limit), use wc -c for bytes, not characters.

Why do emoji count as two characters?

JavaScript strings (and most modern languages) measure length in UTF-16 code units. Most emoji are in the Supplementary Planes and use two code units (a surrogate pair), so "🎉".length === 2. To get the human-intuitive count where one emoji equals one character, you need a grapheme cluster count, which most basic counters don't do. The browser tool may add this as an option later.

How do I count lines that match a pattern?

Best done on the command line. grep -c "ERROR" log.txt returns the number of lines containing ERROR. For Excel, =COUNTIF(A:A, "*pattern*"). For Word, use Find & Replace's Highlight All — the status bar then shows the match count.

Can I count words in many files at once?

On the command line: wc -w *.txt — gives a row per file plus a total. For Word documents, the cleanest path is to convert them to plain text (one batch step in a script) and then use wc; running Word's UI dialog file-by-file is slow.

— the joinlines team, 2026-04-18

§ — PRIVACY Your text never leaves your browser. No upload, no account, no logs on what you paste.
PRIVACY PAGE ↗