Operations 9 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the Linux cut Command: Practical Examples for Text Extraction

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
s

2. 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
ls

3. 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 comma

4. 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
bala

5. 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/bala

To 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/bash

6. 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 output

7. 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/bala

8. 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/bash

9. 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/bash

10. 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 python

These examples demonstrate how mastering cut enables efficient text manipulation in shell scripts and command‑line workflows.

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.

Linuxcommand-lineUnixtext processingShell scriptingcut
Liangxu Linux
Written by

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.)

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.