# Count The Number Of Matches In A Grep

My go to way of counting the number of matches in a `grep` of a file is to pipe it to another command — `wc`.

Here is what that looks like with the README for [this repo](https://github.com/jbranchaud/til). This counts the number of lines that start with `###`.

```bash
$ grep '^###' README.md | wc -l
      48
```

When `wc` is used with the `-l` flag, it gives a count of the number of lines. In this case the number of `grep` matches that get piped to it.

There is another way to do this solely with the `grep` command — using the `-c` flag.

```bash
$ grep -c '^###' README.md
48
```

When you include the `-c` (or `--count`) flag with `grep`, instead of the matches being output, the count of the matches is output.

See `man grep` for more details.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/os/unix/search-query/count-the-number-of-matches-in-a-grep.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
