Fundamentals 9 min read

Mastering the Linux cut Command: Practical Examples and Advanced Options

This tutorial explains the Linux cut command, covering its syntax, key options such as -f, -d, -b, -c, --complement and --output-delimiter, and provides numerous real‑world examples for extracting fields, characters, and bytes from text files and combining cut with other Unix utilities.

ITPUB
ITPUB
ITPUB
Mastering the Linux cut Command: Practical Examples and Advanced Options

Introduction

The cut command is a standard utility on Linux and Unix systems used to extract specific bytes, characters, or fields from each line of a file and write the result to standard output. This guide presents practical examples that can be applied in everyday command‑line workflows.

Basic Syntax

The general form is: cut OPTION... [FILE]... Key options include: -f: select fields (default delimiter is a TAB). -d: specify a custom field delimiter. -b: select bytes, optionally as a range. -c: select characters, also supporting ranges. --complement: output all fields except those specified. --output-delimiter='delimiter': change the output field separator. --only-delimited: omit lines that do not contain the delimiter.

Using Delimiters

Combining -d and -f lets you extract columns based on a chosen separator. For example, to print the first field of /etc/passwd using a colon:

$ cut -d ':' -f1 /etc/passwd
root
bin
daemon
...

To remove the first field from a space‑separated file content.txt:

$ cut -d " " -f 1 content.txt
Ubuntu
Microsoft
OsX
Unix
FreeBSD

Extracting multiple fields, such as the first and sixth fields from /etc/passwd where the line contains /bin/bash:

$ grep "/bin/bash" /etc/passwd | cut -d ':' -f1,6
root:/root
slax:/home/slax

Complementing Selections

The --complement option outputs everything except the specified fields. The following command prints all fields except the second one from /etc/passwd:

$ grep "/bin/bash" /etc/passwd | cut -d ':' --complement -f2
root:0:0:root:/root:/bin/bash

Changing the Output Delimiter

By default, the output delimiter matches the input delimiter. Use --output-delimiter to set a different separator. For instance, replace the colon with a space:

$ cut -d: -f1,7 --output-delimiter ' ' /etc/passwd | sort | uniq -u
_apt /usr/sbin/nologin
backup /usr/sbin/nologin
...

You can also output each field on a new line by specifying $'\n' as the delimiter:

$ grep root /etc/passwd | cut -d ':' -f1,6,7 --output-delimiter=$'
'
root
/root
/bin/bash

Cutting by Character Position

Characters are counted from the start of each line. To print the first character of each line in content.txt:

$ cut -c 1 content.txt
U
M
O
U
F

To display characters 1 through 7:

$ cut -c 1-7 content.txt
Ubuntu
Microso
OsX El
Unix
FreeBSD

Extract from the second character to the end:

$ cut -c2- content.txt
buntu Linux
icrosoft Windows
sX El Capitan
ix
reeBSD

Extract the first four characters:

$ cut -c-4 content.txt
Ubun
Micr
OsX
Unix
Free

Cutting by Byte Position

Use -b with comma‑separated numbers or ranges. To extract the first three bytes of each line:

$ cut -b 1,2,3 content.txt
Ubu
Mic
OsX
Uni
Fre

To extract bytes 1‑3 and 5‑7:

$ cut -b 1-3,5-7 content.txt
Ubutu
Micoso
OsXEl
Uni
FreBSD

Combining cut with Other Commands

Cut becomes especially powerful when piped with other utilities. Example extracting user, PID, and command columns from ps output:

$ ps -L u n | tr -s " " | cut -d " " -f 2,3,14-
USER PID COMMAND
0 676 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
...

Another example retrieves memory statistics and saves them to a file:

$ free -m | tr -s ' ' | sed '/^Mem/!d' | cut -d " " -f2-4 >> memory.txt
# memory.txt now contains: 985 86 234

Conclusion

The cut command works well with many other Linux/Unix tools, allowing you to build pipelines for complex text processing. Its main limitation is the inability to specify multiple character delimiters; in such cases, preprocess the input with tr or similar utilities before using cut.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellcommand-lineUnixtext processingcut command
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.