# awk

* File manipulation
* Reads from std in and writes to std out
* Basically does the operation between `{...}` on every line

## Identity Op

```bash
cat README.md | awk '{print $0}'
OR
awk '{print $0}' README.md
```

## Variables

$0 - WHOLE LINE

$1, $2, $3 - index for line split by whitespace

$NF - last field

$(NF-2)

NR is row number

## Change Field Seperator

Splits on colon for $1, $2, $3

```bash
awk '{print $2}' logs.txt | awk 'BEGIN{FS=":"}{print $1}' | sed 's/\[//'
```

### Get Text between two text

```bash
awk -v FS="(<a>|</a>)" '{print $2}'
```

## If

```bash
awk '{if ($(NF-2) == "200") {print $0}}' logs.txt
```

## Culumulative

```bash
awk '{a+=$(NF-2); print "Total so far:", a}' logs.txt

#only care about final state
awk '{a+=$(NF-2)}END{print "Total:", a}' logs.txt
```

## Other Examples

```bash
awk '{print NR ") " $1 " -> " $(NF-2)}' logs.txt
```


---

# 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://openai.gitbook.io/code-cheatsheets/all/bash/awk.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.
