Line Count

The number of lines in a text. There are two types - logical lines delimited by newline characters and visual lines created by word wrap - and line count is used for calculating character counts, page counts, and measuring code size.

Line count is the number of lines that make up a text. Although it seems straightforward, the definition of a "line" comes in two varieties. A logical line is delimited by a newline character and corresponds to the line numbers shown in a text editor. A visual (or wrapped) line is created when text wraps at the edge of the display area, so the same text can have different visual line counts depending on the window width.

In programming, line count is a fundamental metric for measuring code size. LOC (Lines of Code) is the traditional measure of software scale, and SLOC (Source Lines of Code), which excludes blank lines and comments, is considered more accurate. The Linux kernel contains roughly 30 million lines, and Google's codebase is said to exceed 2 billion.

Text editors and terminals display line counts as a standard feature. VS Code shows the cursor position in "line:column" format in the status bar and can report the total line count of a file. The UNIX command wc -l is the simplest way to count lines in a file. Note, however, that wc -l counts newline characters, so a file whose last line lacks a trailing newline will return a count one less than the actual number of lines.

The relationship between character count and line count is determined by the number of characters per line (line length). Japanese manuscript paper has 20 characters per line across 20 lines, totaling 400 characters. Printing 40 characters by 36 lines on an A4 page yields 1,440 characters per page. Multiplying the average characters per line by the line count gives an approximate character count, though short lines (such as the last line of a paragraph) make the actual count somewhat lower.

In CSV and TSV files, line count corresponds to the number of records. A CSV file with one million lines contains one million data records (excluding the header). However, if a field contains an embedded newline, a naive line count will not match the record count. Accurate record counting requires a CSV parser that correctly handles quoted fields.

In web design, CSS techniques can limit the number of displayed lines. The -webkit-line-clamp property restricts the visible line count and shows an ellipsis (...) for overflow. This is commonly used in card UIs and list views to cap a description at three lines, controlling the displayed amount by line count rather than character count. Text processing books on Amazon explore these techniques further.

Share this article