How to use the CUT command in Linux?

The CUT command is helpful for those processing big data files in Linux. It can help you get the information you need more easily, and also exclude information that you don’t need. If you know how to use the cut command and the sed command, you will be able to make big changes in documents easily. Let us take a closer look at the command right away.

Let us imagine that you have a document looking like this. It is called players.txt.

christiano:ronaldo:38
lionel:messi:35
erling:haaland:24
kylian:mbappe:18

This document is so small that you don’t really need to use any command to make changes as it is faster to do it manually. But if you have 100s of lines in the document, there is a lot of time to save by using the cut command and other commands such as awk and sed.

Using the cut command, let us do some cool things with our players.txt file.

How to use the CUT command in Linux?

Easy commands with the cut command.

cut -b 1 players.txt

This command will print the first bit on each line of the file.

cut -b 1-5 players.txt

This will show you the first five characters of each line in the document.

cut -d ":" -f 1 players.txt

This command is a bit more complicated, but it separates the field from one another with the -d command. I have told it that the : (colon) is the character separating the fields. As a result, it will consider the file to consist of three field, and using the -f option, I tell it to print the first field. The result will look like this.

using the cut command

If you want to show the first and the third field, you can change the command to look like this.

cut -d ":" -f 1,3 players.txt

Let us do one last thing: to change the delimiter, the colon. Maybe you want a space between the names instead of the :. How can that be done?

cut -d ":" -f 1- players.txt --output-delimiter=" "

This will show you the full list with a space in between the names instead of the colon. What if you want to save the output to a new file? Is that possible? Run the following command to save the output of your cut command.

cut -d ":" -f 1- players.txt --output-delimiter=" " > newplayers.txt

If you run the command, the result will be saved to newplayers.txt. You can run the following command to see the outcome.

cat newplayers.txt
using the cut command in ubuntu

There you go. You have now learned how to use some of the functions available with the cut command in Linux. I hope you have found them useful. Would you like to find out which is the best VPN service for Linux Mint and Ubuntu? Or would you like to learn more about other Linux commands? Look around In the IP Address Guide to find the information you are looking for.

Leave a Reply