# awk / sed

## awk

Pattern-directed scanning and processing language. Awk scans each input file for lines that match any of a set of patterns specified literally in prog

```
# print 2nd parameter from output for each line
history | awk '{print $2}'

# find 20 most used commands
# * sort: sort output
# * uniq -c: count unique lines
# * sort -n: sort by line count numerical
# * tail: show last 20 results
history | awk '{print $2}' | sort | uniq -c | sort -n | tail -n 20
```

## sed&#x20;

The sed utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of commands. The input is then written to the standard output.

```
# replace string in file, "-i" updated the file in place
sed -i 's/old_string/new_string/g' input.txt

# find string in all files in the current directory and
# replace it in the output with a new string
grep -ri "old_string" . | sed 's/old_string/new_string/g'
```


---

# 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://marcel-birkner.gitbook.io/awesome-sre/linux-commands/awk-sed.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.
