> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/misc/text-processing/extract-fields-from-lines.md).

# Extract fields from lines

## Extract fields with awk

Example to extract fields with `awk`

```bash
kali@kali:~$ echo "hello::there::friend" | awk -F "::" '{print $1, $3}'
hello friend
```

Note that `awk` supports **multiple** characters as delimiter.

## Extract fields with cut

To extract the usernames and login shells from `/etc/passwd` with `cut`&#x20;

```bash
┌──(kali㉿kali)-[~/OffSec_Cources]
└─$ head /etc/passwd | cut -d ':' -f1,7
root:/usr/bin/zsh
daemon:/usr/sbin/nologin
bin:/usr/sbin/nologin
sys:/usr/sbin/nologin
sync:/bin/sync
games:/usr/sbin/nologin
man:/usr/sbin/nologin
lp:/usr/sbin/nologin
mail:/usr/sbin/nologin
news:/usr/sbin/nologin
```

Note that `cut` only supports a **single** character as delimiter

## Resources

**awk** - Linux manual page: <https://man7.org/linux/man-pages/man1/awk.1p.html>

**cut** - Linux manual page: <https://man7.org/linux/man-pages/man1/cut.1.html>
