# Grab The First Line Of A File

You can grab the first line of a file with `sed` using either the `p` (print) command or the `d` (delete) command.

First, the *print* command can be told to print the line matching the line number `1`. That combined with the `-n` flag, which suppresses all lines not explicitly printed, will print just the first line in the file.

```bash
$ sed '1 p' README.md
# TIL
```

Second, the *delete* command can be told to delete all lines that aren't the first (`1`) line.

```bash
$ sed '1! d' README.md
# TIL
```

The `1` will match on the first line. By following it with `!`, that will negate it so that it represents all lines except `1`.

See `man sed` for more details.

Note: there are more efficient ways, not using `sed`, to get the first line in a file. This is an exercise in using and understanding some `sed` features.


---

# 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/tools/sed/grab-the-first-line-of-a-file.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.
