Master the Linux cut Command: Extract Text Columns Like a Pro
Learn how to use the Linux cut command for powerful text processing, including selecting character columns, ranges, specific fields with delimiters, complementing fields, changing output delimiters, and combining cut with other Unix tools such as grep, sed, and ps.
Linux command cut is used for text processing. You can use this command to extract parts of text from a file by selecting columns.
This article provides practical examples of the cut command that can be used in everyday command-line activities.
For most examples we will use the following test file.
$ 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. Selecting character columns
To extract only the desired columns from a file, use the -c option. The following example shows the second character of each line in test.txt.
$ cut -c2 test.txt
a
p
sAs shown, the characters a, p, s are the second characters of each line in test.txt.
2. Selecting a range of characters
By specifying a start and end position separated by a dash, you can also extract a range of characters. The following example extracts the first three characters of each line in test.txt.
$ cut -c1-3 test.txt
cat
cp
ls3. Selecting characters with a start or end position
You can pass a start or end position to cut using the -c option.
The following specifies only the start position before the dash. This 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 following specifies only the end position after the dash. This example extracts the first 8 characters of each line.
$ cut -c-8 test.txt
cat comm
cp comma
ls commaIf you do not specify a number before or after the dash, the whole line is printed, as shown.
$ 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. Selecting specific fields from a file
If you want to extract whole fields instead of a certain number of characters, combine the -f and -d options. -f specifies the fields to extract, and -d specifies the field delimiter used in the input file.
The following example uses ':' as the delimiter to display only the first field of each line in /etc/passwd, which is the username.
$ cut -d':' -f1 /etc/passwd
root
daemon
bin
sys
sync
games
bala5. Selecting multiple fields from a file
You can also extract multiple fields from a file or standard output. The example below shows the usernames and home directories of users whose login shell is /bin/bash.
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
bala:/home/balaTo display a range of fields, specify the start and end fields as shown. In this example we select fields 1 to 4, 6 and 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 a line contains the delimiter
In our /etc/passwd example, if you specify a delimiter other than ':' (colon), cut will display the whole line even if it does not contain the delimiter.
In the following example we set the delimiter to '|' (pipe), and cut shows the whole line because no line contains '|'.
$ grep "/bin/bash" /etc/passwd | cut -d'|' -f1
root:x:0:0:root:/root:/bin/bash
bala:x:1000:1000:bala,,, :/home/bala:/bin/bashHowever, you can use the -s option to filter and display only lines that contain the specified delimiter.
The following example shows no output because cut did not find any lines with '|' as the delimiter in /etc/passwd.
$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f17. Selecting all fields except specified ones
To complement the field list, use the --complement option.
The following example shows all fields in /etc/passwd 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 is the same as the input delimiter specified with -d.
To change the output delimiter, use the --output-delimiter option. In this example the input delimiter is ':' but the output delimiter is '#'.
$ grep "/bin/bash" /etc/passwd | cut -d':' -s -f1,6,7 --output-delimiter='#'
root#/root#/bin/bash
bala#/home/bala#/bin/bash9. Changing the output delimiter to a newline
In this example each field output by cut is displayed on a separate line. We still use --output-delimiter, but set its value to $'\n' to insert a newline as the delimiter.
$ grep bala /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'
'
bala
/home/bala
/bin/bash10. Combining cut with other Unix commands
When combined with the stdout of other Unix commands, cut can be very powerful.
Once you master the basic usage of cut explained above, you can wisely use it to solve many text manipulation needs.
The following example shows how to extract useful information from the output of the ps command. We also demonstrate using grep and sed to filter ps output before feeding it to cut. Here we use the -d and -f options as explained earlier.
$ ps axu | grep python | sed 's/\s\+/ /g' | cut -d' ' -f2,11-
2231 /usr/bin/python /usr/lib/unity-lens-video/unity-lens-video
2311 /usr/bin/python /usr/lib/unity-scope-video-remote/unity-scope-video-remote
2414 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon
2463 /usr/bin/python /usr/lib/system-service/system-service-d
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
