Mastering the Linux cut Command: Practical Examples for Text Extraction
This guide explains how to use the Linux cut command for extracting specific characters, fields, and ranges from text files, covering options like -c, -f, -d, complement, custom output delimiters, and combining cut with other Unix utilities for powerful text processing.
The cut command is a versatile Linux utility for extracting selected portions of text from files or standard input, useful for column‑wise or field‑wise processing.
1. Selecting character columns
Use the -c option to specify character positions. The example extracts the second character of each line in test.txt:
$ cut -c2 test.txt
a
p
s2. Selecting a range of characters
Specify a start and end position with -c to extract a range. The following command returns the first three characters of each line:
$ cut -c1-3 test.txt
cat
cp
ls3. Selecting from a start or to an end position
Provide only a start or only an end position. Extract from the third character to the end of each line:
$ cut -c3- test.txt
t command for file oriented operations.
command for copy files or directories.
command to list out files and directories with its attributes.Or extract the first eight characters of each line:
$ cut -c-8 test.txt
cat comm
cp comma
ls comma4. Selecting specific fields
Combine -f (fields) with -d (delimiter) to extract whole fields rather than character counts. The example shows the first field (username) from /etc/passwd using : as delimiter:
$ cut -d':' -f1 /etc/passwd
root
daemon
bin
sys
sync
games
bala5. Selecting multiple fields
Extract several fields at once. The command below lists the username (field 1) and home directory (field 6) for users whose login shell is /bin/bash:
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
bala:/home/balaTo select a range plus individual fields, specify the range and additional numbers, e.g., fields 1‑4, 6, 7:
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7
root:x:0:0:root:/root:/bin/bash
bala:x:1000:1000:bala,,,:/home/bala:/bin/bash6. Selecting fields only when the delimiter is present
By default, cut prints the whole line if the delimiter is not found. Use -s to suppress lines without the delimiter.
$ grep "/bin/bash" /etc/passwd | cut -d'|' -f1 # prints whole lines because '|' is absent
$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f1 # produces no output7. Selecting all fields except specified ones
Use the --complement option to invert the field list. The example shows all fields except field 7:
$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -s -f7
root:x:0:0:root:/root
bala:x:1000:1000:bala,,,:/home/bala8. Changing the output delimiter
By default, the output delimiter matches the input delimiter. Override it with --output-delimiter. The following changes the delimiter from : to #:
$ grep "/bin/bash" /etc/passwd | cut -d':' -s -f1,6,7 --output-delimiter='#'
root#/root#/bin/bash
bala#/home/bala#/bin/bash9. Using a newline as the output delimiter
Set the output delimiter to a newline with $'\n' so each selected field appears on its own line:
$ grep bala /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'
'
bala
/home/bala
/bin/bash10. Combining cut with other Unix commands
Pipe the output of commands such as ps, grep, and sed into cut for refined extraction. Example extracting the second and eleventh columns from ps axu output after filtering for python processes:
$ ps axu | grep python | sed 's/\s\+/ /g' | cut -d' ' -f2,11
2231 /usr/bin/python /usr/lib/unity-lens-video
2311 /usr/bin/python /usr/lib/unity-scope-video-remote
2414 /usr/bin/python /usr/lib/ubuntuone-client/syncdaemon
2463 /usr/bin/python /usr/lib/system-service-system-service-d
3274 grep --color=auto pythonThese examples demonstrate how mastering cut enables efficient text manipulation in shell scripts and command‑line workflows.
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.
