Or copy link
Copy link
The Linux cut command is a powerful utility that allows users to extract specific portions of text from a file or data stream. It is particularly useful for data manipulation and filtering. It can also be used to cut parts of a line by field, delimiter, byte position, and character. These features make it an important tool for text processing in shell scripts or command-line operations.
In this tutorial, we will explore the several aspects of cut command in Linux with possible examples.
The cut command in Linux is a powerful tool used to extract particular portions of files. It operates by selecting sections of text based on bytes, characters, or delimited fields. It is commonly used for creating bash scripts and command-line utilities to manipulate text data and extract useful information.
With its simple syntax and versatile options, the cut command provides an essential functionality for text processing tasks. The general syntax of the cut command is given below:
cut [OPTION] [FILE]
The [OPTION] specifies how the cut should be performed, and [FILE] is the name of the file to process.
The cut command can be used with various options to specify how the data should be cut. These options include:
Practice Commands on Our best Linux VPS!
Ultahost provides Linux hosting with NVMe SSD storage. Use our Cheap Linux VPS to practice the cut command and efficiently manipulate text data in your projects.
Let’s explore some practical examples of how the cut command can be used:
To extract specific bytes from a line, you can use the -b option. For example, to extract the first five bytes of each line from a file, use the below command:
cut -b 1-5 file.txt
This extracts the first 5 bytes from each line of file.txt.
file.txt
If you want to extract certain characters, you can use the -c option. Let’s extract the first character of each line of the given file:
cut -c 1 file.txt
This extracts the first characters H, H, U, and T from each line of file.txt.
Extracting a Range of Characters
To extract the range of characters, use the specific range such as 1 to 10 with the -c option:
cut -c 1-10 file.txt
This extracts characters 1 to 10 from each line of file.txt.
Extracting Multiple Ranges of Characters
You can specify several ranges by separating them with a comma. For example, to keep characters from 1 to 10 and 20 to 30:
cut -c 1-10,20-30 file.txt
It extracts multiple ranges of characters from the file.txt.
The -f option is particularly useful when dealing with delimited data. To select the second field of each line using a comma as a delimiter, execute the below command:
cut -d ',' -f 2 file.txt
This extracts the second field from each line of file.txt, assuming the delimiter is a comma.
Extracting Multiple Fields
Let’s extract the second and fourth fields from each line, assuming the delimiter is a comma:
cut -d "," -f 2,4 file.txt
It extracts multiple fields from the file.txt.
Extracting a Range of Field
You can also extract a range of fields by specifying the file name. Let’s extract fields 2 to 4 from each line, skipping field 1:
cut -d " " -f 2-4 file.txt
This extracts fields from 2 to 4 of file.txt.
To change the output delimiter, use the –output-delimiter option. For instance, change the output delimiter comma to a semicolon via the below command:
cut -d ',' -f 1,2 --output-delimiter=';' file.txt
The output shows that the output delimiter comma has been changed to a semicolon.
Suppressing Lines Without a Delimiter
To extract the first field only from lines that contain a comma, use the below command:
cut -d "," -s -f 1 file.txt
This extracts the first field from the file.txt, but only if the line contains a comma.
The cut command can also be used with piped input to find a specific pattern. For example, to get the first field of all users on a system, use the getent with the cut command as below:
getent passwd | cut -d ':' -f1
This extracts the list of Linux users on Ubuntu from the first field of each line.
Using Complement Option
The –complement option displays everything except the specified fields, bytes, or characters. Let’s show everything except the first field having comma delimiter:
cut -d ',' --complement -f1 file.txt
The output shows that the first line is skipped due to the delimiter comma.
Read also Exploring the at Command in Linux
By understanding these examples, you can effectively use the cut command to manipulate text data in your Linux environment. To explore more about the cut command, use the –help utility below:
cut --help
In Linux, the cut command is straightforward and enables you to specify the exact data you wish to isolate, such as a range of characters or fields separated by a specific delimiter. Whether you are working with CSV files, log files, or any other text-based data, cut provides a simple and powerful way to achieve your text processing goals. In addition, the cut command is versatile and can be combined with other commands such as the grep command Linux or awk to perform complex text manipulation tasks.
The cut command is a powerful tool for extracting and manipulating text in files. You can practice using the cut command on Ultahost’s Free VPS hosting, giving you a great opportunity to experiment without any upfront costs. We offer a variety of VPS plans to choose from, so you can find one that meets your needs.
The Linux cut command usage is so crucial that allows users to extract specific portions of text from files or data streams. It is particularly useful for parsing structured text files like CSVs.
The cut command operates by selecting sections of text based on bytes, characters, or delimited fields.
To extract specific fields from a file, use the -f option to specify the field numbers and the -d option to set the delimiter (usually a comma) cut -d ‘,’ -f 2,4 myfile.txt. This extracts the second and fourth fields from each line of myfile.txt.
To specify a range of characters, utilize the -c option. For example, to extract characters 3 to 7 from each line use the command cut -c 3-7 myfile.txt.
The cut command in Bash script is used to extract sections of text from each line of files or data streams. It can process text by bytes, characters, or delimited fields. For example, to select characters from a string, you can use echo ‘sample text’ | cut -c 1-6, which will output a sample.
Use the –output-delimiter option to specify a different delimiter for the output such as cut -d ‘,’ -f 1,2 –output-delimiter=’:’ myfile.txt.
Yes, you can pipe the output of the cut to other commands for further processing. For instance, searches for a pattern in new_file.txt and extracts the second field of matching lines through the grep “pattern” new_file.txt | cut -d ‘:’ -f 2.
Kali Linux is a popular operating system used for secur...
The chsh command a utility commonly found in Linux dist...
Efficiently managing file systems is crucial in Linux. ...
The .deb packages are the standard format for installin...
For ethical hackers and security enthusiasts, Kali Linu...
October CMS is a flexible, open-source content manageme...
Save my name, email, and website in this browser for the next time I comment.
Δ