# sed, awk & Piping — Linux

Source: https://www.geekswithgeeks.com/en/linux/linux-sed-awk-pipes

> Transform text and chain commands together.

## sed — substitute text

`sed 's/old/new/'` replaces the first match per line; add `g` to replace all matches.

```bash
echo "cat sat on mat" | sed 's/at/og/'
```

Output:

```
cog sat on mat
```

## awk — pull out a field

`awk` splits each line into fields by whitespace (`$1`, `$2`, ...) and lets you print or process them.

```bash
ps aux | awk '{print $2, $11}'
```

Output:

```
1 /sbin/init
842 /usr/bin/bash
```

## Piping commands together

`|` sends one command's output as the next command's input, letting small tools combine into powerful pipelines.

```bash
cat access.log | grep "404" | wc -l
```

Output:

```
37
```

**Quiz:** What does `sed 's/foo/bar/g'` do?

- [ ] Replaces only the first 'foo' per line
- [x] Replaces every 'foo' with 'bar' on each line
- [ ] Deletes lines containing 'foo'

*Answer:* Replaces every 'foo' with 'bar' on each line. The `g` flag makes the substitution global — all matches per line, not just the first.
