Master the Linux ‘cut’ Command: Practical Examples and Advanced Options
This guide explains the Linux cut command, covering its basic syntax, essential options such as field, delimiter, byte and character extraction, and advanced features like complement and custom output delimiters, with step‑by‑step examples and notes on limitations.
Introduction
The cut command extracts selected portions of each line from a file or standard input, using bytes, characters, or fields separated by a delimiter. It is a fundamental tool for text processing in Linux and Unix environments.
Basic Syntax
$ cut OPTION... [FILE]...Common Options
-f: extract specified fields; the default delimiter is a Tab. -d: define a custom delimiter instead of the default Tab. -b: extract specified bytes; ranges can be given with a hyphen. -c: extract specified characters; accepts comma‑separated lists or hyphen‑separated ranges. --complement: output all fields except those selected (inverse selection). --output-delimiter: set a different delimiter for the output. --only-delimited: suppress lines that do not contain the delimiter.
Sample Files
Two example files are used throughout the examples:
$ cat content.txt
Ubuntu Linux
Microsoft Windows
OsX El Capitan
Unix
FreeBSD # /etc/passwd (excerpt)
root:x:0:0:root:/root:/bin/bash
alvin:x:1000:1000:alvin:/home/alvin:/bin/bashExamples
Extract the first field of /etc/passwd using a colon as delimiter:
$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...Extract the first field of content.txt using a space as delimiter:
$ cut -d " " -f1 content.txt
Ubuntu
Microsoft
OsX
Unix
FreeBSDCombine fields 1 and 6 from lines containing /bin/bash in /etc/passwd :
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
alvin:/home/alvinSelect a range of fields (1‑4,6,7) from /etc/passwd :
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7
root:x:0:0:/root:/bin/bash
alvin:x:1000:1000:/home/alvin:/bin/bashUse --complement to output all fields except the second one:
$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -f2
root:0:0:root:/root:/bin/bashChange the output delimiter to a space:
$ cut -d: -f1,7 --output-delimiter ' ' /etc/passwd | sort | uniq -u
_apt /usr/sbin/nologin
backup /usr/sbin/nologin
bin /usr/sbin/nologin
...Set the output delimiter to a newline (each field on its own line):
$ grep root /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'
'
root
/root
/bin/bashExtract characters by position:
$ cut -c 1 content.txt
U
M
O
U
F $ cut -c 1-7 content.txt
Ubuntu
Microso
OsX El
Unix
FreeBSDExtract bytes:
$ cut -b 1,2,3 content.txt
Ubu
Mic
OsX
Uni
FreCombine cut with other commands (e.g., ps , free ) for more complex pipelines:
# Extract USER, PID, COMMAND from ps output
ps -L u n | tr -s " " | cut -d " " -f2,3,14-
USER PID COMMAND
0 676 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
... # Save total, used, free memory values to a file
free -m | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2-4 >> memory.txt
cat memory.txt
985 86 234Limitations
The cut command cannot specify multiple characters as a delimiter; consecutive spaces are treated as separate delimiters. To handle such cases, preprocess the input with tr or similar tools before using cut.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
