Mastering Linux cut: 10 Practical Ways to Extract Text Efficiently
This tutorial explains how to use the Linux cut command for text processing, covering character and field selection, range specifications, output delimiters, and integration with other Unix tools through clear examples and code snippets.
Linux command cut is used for text processing, allowing extraction of specific columns from files.
This article provides practical examples of cut usage in everyday command-line activities.
<code>$ cat test.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out files and directories with its attributes.</code>1. Select character columns
Use the
-coption to extract desired columns. The example shows the second character of each line in
test.txt.
<code>$ cut -c2 test.txt
a
p
s</code>The output displays the characters
a,
p,
sas the second character of each line.
2. Use range to select character columns
Specify start and end positions with
-cusing a hyphen. The example extracts the first three characters of each line.
<code>$ cut -c1-3 test.txt
cat
cp
ls</code>3. Use start or end position only
Provide only a start or an end position with
-c. The first example extracts from the third character to the end of each line.
<code>$ 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.</code>The second example extracts the first eight characters of each line.
<code>$ cut -c-8 test.txt
cat comm
cp comma
ls comma</code>If neither start nor end is specified, the whole line is printed.
<code>$ cut -c- test.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out files and directories with its attributes.</code>4. Select specific fields
Combine
-fand
-dto extract fields separated by a delimiter. The example uses ':' to display the first field (username) from
/etc/passwd.
<code>$ cut -d':' -f1 /etc/passwd
root
daemon
bin
sys
sync
games
bala</code>5. Select multiple fields
Extract multiple fields, for example, the username and home directory of users whose login shell is
/bin/bash.
<code>$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:x:0:0:root:/root:/bin/bash
bala:x:1000:1000:bala,,,:/home/bala:/bin/bash</code>6. Select fields only when line contains delimiter
Use the
-soption to suppress lines that do not contain the specified delimiter.
<code>$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f1</code>Since the delimiter '|' does not appear in the file, no output is produced.
7. Complement: select all fields except specified
Use
--complementto exclude fields; the example shows all fields except field 7.
<code>$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -f7
root:x:0:0:root:/root
bala:x:1000:1000:bala,,,:/home/bala</code>8. Change output delimiter
By default the output delimiter matches the input delimiter; use
--output-delimiterto change it, e.g., to '#'.
<code>$ grep "/bin/bash" /etc/passwd | cut -d':' -s -f1,6,7 --output-delimiter='#'
root# /root# /bin/bash
bala# /home/bala# /bin/bash</code>9. Use newline as output delimiter
Set the output delimiter to a newline with
$'\n'to print each field on a separate line.
<code>$ grep bala /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n'
bala
/home/bala
/bin/bash</code>10. Combine cut with other Unix commands
Cut can be combined with pipelines to process command output, such as extracting useful columns from
psoutput after filtering with
grepand
sed.
<code>$ ps axu | grep python | sed 's/\s\+/ /g' | cut -d' ' -f2,11
2231 /usr/bin/python ...
2311 /usr/bin/python ...
2414 /usr/bin/python ...
2463 /usr/bin/python ...
3274 grep --color=auto python</code>Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.