Text-Fu · Lesson 15 of 16
wc and nl
0 / 5 checkpoints answered
The wc command counts properties of text streams, while nl writes input with generated line numbers. Both read files or stdin and send their results to stdout.
Reading the Default wc Output
With no count option, wc prints the number of newline characters, words, and bytes, followed by the filename when one was supplied:
$ printf 'red blue\ngreen\n' > colors.txt
$ wc colors.txt
2 3 15 colors.txt
From left to right:
2newline characters, reported as lines.3whitespace-delimited words.15bytes in this ASCII example.
A final text line without a terminating newline is not counted by wc -l, because that option counts newline characters rather than visually perceived lines.
CheckIn the default output from wc file.txt, what do the first three numbers represent?
press 1/2/3 to answer · → next lesson
Requesting One Count
Select only the measurement you need:
-l: Count newline characters.-w: Count words.-c: Count bytes.-m: Count characters according to the current locale.
For example:
$ wc -w colors.txt
3 colors.txt
Byte and character counts are equal for ASCII text but can differ for multibyte encodings such as UTF-8. When stdin is used without a filename operand, wc normally omits a filename label:
$ printf 'one two\n' | wc -w
2
CheckWhich command reports only the word count for essay.txt?
press 1/2/3 to answer · → next lesson
CheckWhich option asks wc to count characters rather than bytes in the current locale?
press 1/2/3 to answer · → next lesson
When multiple files are named, wc prints one result per file and a total line. GNU wc -L reports the maximum display width of an input line.
Numbering Nonempty Lines with nl
By default, nl numbers nonempty lines in the logical body of its input. Suppose notes.txt contains a blank second line:
alpha
beta
The blank line is preserved but receives no number:
$ nl notes.txt
1 alpha
2 beta
nl writes numbered output; it does not modify notes.txt.
CheckHow does nl notes.txt handle blank body lines by default?
press 1/2/3 to answer · → next lesson
Numbering Every Line
Use -ba to select body numbering style a, which numbers all lines:
$ nl -ba notes.txt
1 alpha
2
3 beta
Other options control formatting. For example, -w 3 sets the number field width and -s ': ' changes the separator after the number.
CheckWhich command numbers every body line in notes.txt, including blank lines?
press 1/2/3 to answer · → next lesson
To practice counting and numbering text, try these hands-on labs:
- Linux wc Command: Text Counting - Practice counting words, lines, and characters in text files using the
wccommand. - Linux nl Command: Line Numbering - Learn to number lines in text files with the
nlcommand. - Word Count and Sorting - Apply your knowledge of
wcto count lines, words, and characters, and combine it with sorting for practical text analysis tasks.
Summary
You can now measure text streams and add visible line numbers without editing the source.
- Interpret the default lines, words, and bytes columns from
wc. - Select one count with
-l,-w,-c, or-m. - Distinguish byte counts from character counts.
- Number nonempty lines with default
nlbehavior. - Number blank lines too with
nl -ba.
Lesson complete
You finished wc and nl. Nice work — keep the momentum going.