Unix Commands  «Prev  Next»
Lesson 8 The –c option to grep
Objective Use grep/ Count number of lines matching pattern.

-c option to grep

The `grep` command with the `-c` (count) option is used to count the number of lines that match a given pattern in a file or input stream.
Syntax:
grep -c "pattern" filename

Explanation:
  • grep searches for the specified "pattern" in the filename.
  • -c counts the number of matching lines and outputs the count instead of the actual matching lines.

Example 1: Counting Lines Containing a Specific Word
Command:
grep -c "error" logfile.txt

Output (Example):
5

This means there are "5 lines" in `logfile.txt` that contain the word `"error"`.
Example 2: Counting Lines from `stdin` (Piped Input)
You can use `grep -c` with a pipeline:
dmesg | grep -c "usb"

This counts the number of lines in the system log (`dmesg`) that contain `"usb"`.
Example 3: Counting Case-Insensitive Matches
To ignore case, add the `-i` option:
grep -i -c "warning" logfile.txt
This counts both `"Warning"`, `"WARNING"`, and `"warning"`.
Example 4: Counting Lines in Multiple Files
grep -c "failed" /var/log/auth.log /var/log/syslog

Output (Example):
/var/log/auth.log:3
/var/log/syslog:7
Each file's matching line count is displayed.
Alternative: Counting Exact Matches with `-w`
To count "only whole word matches", use:
grep -w -c "fail" logfile.txt

This ensures `"fail"` is matched as a standalone word and not as part of `"failed"`.


Count the number of lines matching a Pattern

Use grep to count the number of lines matching a pattern. You can use the –c option with grep to display the lines that match your search parameters. Here is additional information about regular expression wildcards.
-C NUM
--context=[NUM]
     (GNU Extension)
     Print NUM lines (default 2) of output context.

`-c'
`--count'
     Suppress normal output; instead print a count 
   of matching linesfor each input file.  
   With the `-v', `--invert-match' option,
     count non-matching lines.    
  • Regular Expression Wildcards:
    Regular expressions are patterns used in UNIX commands to match the contents of a file. Regular expression wildcards, also called metacharacters, are used to define the search parameters.
    They include the following:
    1. ^ Beginning of Line (Must be first character in your pattern)
    2. $ End of Line (Must be the last character in your pattern )
    3. . Any one character
    4. [aprw] Matches one character from the listed set. This notation is called a character class.
    5. [a-c4-8] The listed set may include ranges of characters. Ranges are indicated using a dash between two characters. This set includes all the characters from a to c, plus the digits from 4 to 8.
    6. [^a-r] You can reverse the meaning of the set by inserting a caret as the first character. This matches any one character except a through r.
Design of Unix OS

Matching Any Character

You could also match those pesky hyphens with a dot (.):
\d\d\d.\d\d\d.\d\d\d\d

The dot or period essentially acts as a wildcard and will match any character (except, in certain situations, a line ending). In the example above, the regular expression matches the hyphen, but it could also match a percent sign (%):
707%827%7019

Or a vertical bar (|):
707|827|7019

Or any other character. The dot character (officially, the full stop) will not normally match a new line character, such as a line feed (U+000A). However, there are ways to make it possible to match a newline with a dot, which I will show you later. This is often called the dotall option.
  • Regular Expression:
    \d\d\d.\d\d\d.\d\d\d\d
    

    • \d: Matches any digit (0–9).
    • \d\d\d: Matches exactly three digits.
    • .: Matches any single character (except a newline, unless specific flags like DOTALL are used, which isn’t the case here by default).
    • \d\d\d: Matches another three digits.
    • .: Matches any single character again.
    • \d\d\d\d: Matches exactly four digits.
    Total Length and Pattern
    This regex matches a string of 11 characters:
    • 3 digits, 1 wildcard, 3 digits, 1 wildcard, 4 digits.

    Examples:
    1. Hyphen case: 707-827-7019
      • 707 (three digits)
      • - (any character, matched by .)
      • 827 (three digits)
      • - (any character, matched by .)
      • 7019 (four digits)
      • This matches perfectly.
    2. Percent sign case: 707%827%7019
      • 707 (three digits)
      • % (any character, matched by .)
      • 827 (three digits)
      • % (any character, matched by .)
      • 7019 (four digits)
      • This also matches perfectly.
    3. Vertical bar case: 707|827|7019
      • 707 (three digits)
      • | (any character, matched by .)
      • 827 (three digits)
      • | (any character, matched by .)
      • 7019 (four digits)
      • This matches as well.

    Additional Notes
    • The . in a regular expression is indeed a wildcard that matches any single character (except newlines in most regex engines by default). Your examples correctly illustrate this flexibility.
    • The regex doesn’t enforce what the separator is—it could be a hyphen (-), percent (%), vertical bar (|), space ( ), or even a letter or number (e.g., 707x827y7019 would still match).
    • If you wanted to restrict the separator to specific characters (e.g., only hyphens), you’d need to replace . with something like [-] (a character class allowing only a hyphen).

    Conclusion: It will match any string with the pattern of three digits, any character, three digits, any character, and four digits, as demonstrated by the examples listed above.

The next lesson demonstrates how to redirect output with the tee command[1].

coption grep - Exercise

Click the Exercise link below to practice using the grep command.
coption grep - Exercise

[1]command: If set, holds the command passed to the shell with the -c option.

SEMrush Software 8 SEMrush Banner 8