# Getting the line number of the nth match

If we're aiming to find the line number of the nth match, we might think to use `grep` and parse it's output.

Take this markdown document for example, where we want to find the line number of the second occurrence of `---`:

*input.md*

```
---
title: some frontmatter
--- <-- We want the line number of this line

# Some content
```

```bash
grep '^---$' -m 2 -n input.md | tail -1 | cut -d ":" -f 1
```

This gets the job done, but it invokes a number of subshells, reducing performance. An alternative using `awk` may be less I/O intensive:

```bash
awk '/^---$/ && (++c == 2) { print NR; exit }' input.md
```

[Source](https://stackoverflow.com/questions/57044203/how-to-get-the-line-number-of-nth-match#comment100617725_57044437)


---

# 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://notes.eliasnorrby.com/bash/get-line-number-of-nth-match.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.
