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.
$ 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.1. Select character columns
Use the -c option to extract desired columns. The example shows the second character of each line in test.txt.
$ cut -c2 test.txt
a
p
sThe output displays the characters a, p, s as the second character of each line.
2. Use range to select character columns
Specify start and end positions with -c using a hyphen. The example extracts the first three characters of each line.
$ cut -c1-3 test.txt
cat
cp
ls3. 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.
$ 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.The second example extracts the first eight characters of each line.
$ cut -c-8 test.txt
cat comm
cp comma
ls commaIf neither start nor end is specified, the whole line is printed.
$ 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.4. Select specific fields
Combine -f and -d to extract fields separated by a delimiter. The example uses ':' to display the first field (username) from /etc/passwd.
$ cut -d':' -f1 /etc/passwd
root
daemon
bin
sys
sync
games
bala5. Select multiple fields
Extract multiple fields, for example, the username and home directory of users whose login shell is /bin/bash.
$ 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/bash6. Select fields only when line contains delimiter
Use the -s option to suppress lines that do not contain the specified delimiter.
$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f1Since the delimiter '|' does not appear in the file, no output is produced.
7. Complement: select all fields except specified
Use --complement to exclude fields; the example shows all fields except field 7.
$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -f7
root:x:0:0:root:/root
bala:x:1000:1000:bala,,,:/home/bala8. Change output delimiter
By default the output delimiter matches the input delimiter; use --output-delimiter to change it, e.g., to '#'.
$ grep "/bin/bash" /etc/passwd | cut -d':' -s -f1,6,7 --output-delimiter='#'
root# /root# /bin/bash
bala# /home/bala# /bin/bash9. Use newline as output delimiter
Set the output delimiter to a newline with $'\n' to print each field on a separate line.
$ grep bala /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'
'
bala
/home/bala
/bin/bash10. Combine cut with other Unix commands
Cut can be combined with pipelines to process command output, such as extracting useful columns from ps output after filtering with grep and sed.
$ 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 pythonSigned-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.
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.
