# cut — Extract Fields, Characters and Bytes from Text Lines

> Practical guide to cut — extract specific fields, characters or byte ranges from text files and piped output, with delimiters, ranges and complement.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/cut/

<!-- PROSE:intro -->
cut is the lean tool for slicing exactly the column you need out of every line – whether that is a field in a CSV file, a character range in a log line, or a byte offset in fixed-width data. You tell it whether to cut by fields (`-f`), characters (`-c`) or bytes (`-b`), and cut returns the rest line by line. By default it splits fields on the tab character; with `-d` you set your own delimiter. In pipelines, cut is often the fastest answer when awk would be overkill.
<!-- PROSE:intro:end -->

## Field Extraction

`cut -f <n> <file>` — Extract the nth field (tab-delimited by default).

```bash
cut -f 2 data.tsv
```

`cut -f <n> -d '<delim>' <file>` — Extract the nth field using a custom delimiter.

```bash
cut -f 1 -d ',' users.csv
```

`cut -f <n>,<m> -d '<delim>' <file>` — Extract multiple specific fields.

```bash
cut -f 1,3 -d ':' /etc/passwd
```

`cut -f <n>-<m> -d '<delim>' <file>` — Extract a range of fields.

```bash
cut -f 2-4 -d ',' data.csv
```

`cut -f <n>- -d '<delim>' <file>` — Extract from field n to the end.

```bash
cut -f 3- -d ':' /etc/passwd
```

`cut -f -<n> -d '<delim>' <file>` — Extract from the first field up to field n.

```bash
cut -f -3 -d ',' data.csv
```

`cut -f <n> -d '<delim>' -s <file>` — Suppress lines that do not contain the delimiter.

```bash
cut -f 2 -d ',' -s mixed.txt
```

`cut -f <n> -d '<delim>' --output-delimiter='<sep>'` — Use a different delimiter for the output.

```bash
cut -f 1,3 -d ':' --output-delimiter=' | ' /etc/passwd
```

## Character Extraction

`cut -c <n> <file>` — Extract the nth character from each line.

```bash
cut -c 1 file.txt
```

`cut -c <n>-<m> <file>` — Extract a range of characters.

```bash
cut -c 1-10 file.txt
```

`cut -c <n>,<m>,<o> <file>` — Extract specific character positions.

```bash
cut -c 1,5,10 file.txt
```

`cut -c <n>- <file>` — Extract from character n to the end of each line.

```bash
cut -c 5- file.txt
```

`cut -c -<n> <file>` — Extract the first n characters of each line.

```bash
cut -c -20 file.txt
```

## Byte Extraction

`cut -b <n> <file>` — Extract the nth byte from each line.

```bash
cut -b 1 file.txt
```

`cut -b <n>-<m> <file>` — Extract a range of bytes (useful for fixed-width binary data).

```bash
cut -b 1-16 binary.dat
```

`cut -b <n>- <file>` — Extract from byte n to the end.

```bash
cut -b 5- data.bin
```

## Complement

`cut --complement -f <n> -d '<delim>' <file>` — Extract all fields EXCEPT the specified ones.

```bash
cut --complement -f 2 -d ',' data.csv
```

`cut --complement -c <n>-<m> <file>` — Remove a range of characters, keep everything else.

```bash
cut --complement -c 1-5 file.txt
```

## Common Patterns

`cut -f 1 -d ':' /etc/passwd` — Extract all usernames from /etc/passwd.

```bash
cut -f 1 -d ':' /etc/passwd
```

`echo $PATH | cut -f 1 -d ':'` — Get the first directory in the PATH variable.

```bash
echo $PATH | cut -f 1 -d ':'
```

`cut -f 1,3 -d ':' /etc/passwd | sort` — Extract username and UID, then sort.

```bash
cut -f 1,3 -d ':' /etc/passwd | sort -t ':' -k 2 -n
```

`ps aux | cut -c 1-80` — Truncate process listing to 80 characters wide.

```bash
ps aux | cut -c 1-80
```

`<command> | cut -f <n> -d ' '` — Extract a column from space-delimited command output.

```bash
df -h | cut -c 1-20,45-52
```

`rev <file> | cut -f 1 -d '.' | rev` — Extract file extensions by reversing, cutting, and reversing again.

```bash
ls | rev | cut -f 1 -d '.' | rev
```

`head -1 data.csv | cut -f 1- -d ',' --output-delimiter=$'\n'` — List CSV column headers one per line.

```bash
head -1 data.csv | cut -f 1- -d ',' --output-delimiter=$'\n'
```

<!-- PROSE:outro -->
## Conclusion

cut wins through simplicity: when your data has a clear, consistent delimiter, you extract the column you want faster and more readably than with any other tool. Mind its limits, though – cut knows only a single-character delimiter (it cannot treat repeated spaces as one, the way awk does) and cannot reorder fields. For multiple consecutive spaces, squeeze them first with `tr -s ' '`; as soon as logic, conditions or variable separators come into play, awk is the better choice. With multibyte (UTF-8) characters, `-c` and `-b` differ – cut by character, not by byte, in that case.

## Further Reading

- [GNU coreutils: cut](https://www.gnu.org/software/coreutils/manual/html_node/cut-invocation.html) – official reference for all options
- [cut(1) man page](https://man7.org/linux/man-pages/man1/cut.1.html) – the Linux manual page for cut
<!-- PROSE:outro:end -->

## Related Commands

- [awk](https://www.jpkc.com/db/en/cheatsheets/files-text/awk/) – powerful text processing with fields, conditions and arithmetic
- [paste](https://www.jpkc.com/db/en/cheatsheets/files-text/paste/) – merge lines of several files column by column
- [column](https://www.jpkc.com/db/en/cheatsheets/files-text/column/) – format input into clean, aligned columns and tables

