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.
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
FreeBSDExtracting 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/slaxComplementing 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/bashChanging 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/bashCutting 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
FTo display characters 1 through 7:
$ cut -c 1-7 content.txt
Ubuntu
Microso
OsX El
Unix
FreeBSDExtract from the second character to the end:
$ cut -c2- content.txt
buntu Linux
icrosoft Windows
sX El Capitan
ix
reeBSDExtract the first four characters:
$ cut -c-4 content.txt
Ubun
Micr
OsX
Unix
FreeCutting 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
FreTo extract bytes 1‑3 and 5‑7:
$ cut -b 1-3,5-7 content.txt
Ubutu
Micoso
OsXEl
Uni
FreBSDCombining 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 234Conclusion
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.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
