wc and nl
100%

Text-Fu · Lesson 15

wc and nl

Learn how to count lines, words, bytes, or characters with wc and number lines with nl.

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:

  1. 2 newline characters, reported as lines.
  2. 3 whitespace-delimited words.
  3. 15 bytes 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.

In the default output from wc file.txt, what do the first three numbers represent?

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

Which command reports only the word count for essay.txt?

Which option asks wc to count characters rather than bytes in the current locale?

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.

How does nl notes.txt handle blank body lines by default?

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.

Which command numbers every body line in notes.txt, including blank lines?

To practice counting and numbering text, try these hands-on labs:

  1. Linux wc Command: Text Counting - Practice counting words, lines, and characters in text files using the wc command.
  2. Linux nl Command: Line Numbering - Learn to number lines in text files with the nl command.
  3. Word Count and Sorting - Apply your knowledge of wc to count lines, words, and characters, and combine it with sorting for practical text analysis tasks.

Lesson complete

You finished wc and nl

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 nl behavior.

  • Number blank lines too with nl -ba.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Text-Fu