UNIX text utilities: Which wc option reports only the number of lines in a file (line count output)?

Difficulty: Easy

Correct Answer: wc -l

Explanation:


Introduction / Context:
The wc (word count) utility summarizes text by reporting lines, words, and bytes. When scripting or auditing logs, you often need just the number of lines. Knowing the correct option avoids parsing extra fields.



Given Data / Assumptions:

  • wc supports multiple flags to control what counts are shown.
  • We want line count only.
  • Input is a regular text file or standard input.


Concept / Approach:
By default, wc filename prints three numbers: lines, words, and bytes. The -l option restricts output to the line count. Other options are -w for words and -c for bytes (or -m for characters on some systems).



Step-by-Step Solution:

To count lines: wc -l file.txtTo count words: wc -w file.txtTo count bytes: wc -c file.txtTo count lines from a pipeline: grep error app.log | wc -l


Verification / Alternative check:
Use nl -ba file.txt | tail -1 to verify the highest printed line number equals the line count. Alternatively, awk 'END{print NR}' file.txt also returns the number of lines.



Why Other Options Are Wrong:
-r: Not a standard wc option. -w: word count, not line count. -c: byte count. None of the above: incorrect because -l is correct.



Common Pitfalls:
Using wc without options and then misreading the columns; confusing bytes and characters on multibyte encodings; counting lines in files that lack a trailing newline (tools may differ in edge handling).



Final Answer:
wc -l

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion