cut Command in Linux
Overview
In Linux, cut is a command-line utility used for selecting parts of lines or fields from a file. The command is useful when you want to extract specific data from a file or a stream of data. This article will explain how to use the cut command, its syntax, and some examples of its usage.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Linux cut Command Syntax
The syntax for the cut command is as follows:
cut Command Options:
- -c or --characters: This option is used to select specific characters from each line of the file. A comma-separated list of character positions is provided after the option to specify which characters to select. For example, -c 1-5 will select the first five characters of each line.
- -d or --delimiter: This option is used to specify the delimiter character used to separate fields in the file. For example, -d ',' will use the comma character as the delimiter.
- -f or --fields: This option is used to select specific fields from each line of the file. A comma-separated list of field positions is provided after the option to specify which fields to select. For example, -f 2,4 will select the second and fourth fields of each line.
- --complement: This option is used to select all the characters or fields except the ones specified using the -c or -f option. For example, -c 1-5 --complement will select all the characters except the first five characters of each line.
Example Usages
-
Selecting specific characters from a file.:
Output:
Explanation: This command will select the first five characters of each line in the example.txt file.
-
Selecting specific fields from a file.:
Output:
Explanation: This command will select the second and fourth fields of each line in the example.csv file. The delimiter used to separate fields is the comma
-
Selecting all characters except specified ones.:
Output:
Explanation: This command will select all the characters except the first five characters of each line in the example.txt file.
Turn Learning into Career Growth
Tips
-
The cut command works on both files and standard input.
-
The delimiter option is often used with the fields option to select specific fields based on the delimiter.
-
The characters option can be used to select a range of characters using the hyphen (-) separator.
Advanced Use Cases of cut Command in Linux
-
Selecting specific fields from a file with a custom delimiter.:
Output:
Explanation: This command will select the first, third, fourth, and fifth fields of each line in the example.txt file. The delimiter used to separate fields is the colon character.
-
Using cut with other commands.:
Output:
Explanation: This command will pipe the output of the cat command to the cut command, which will then select the first five characters of each line.
-
Selecting fields from a file with variable-length fields.:
Output:
Explanation: This command will select the first field and all fields from the third field onwards for each line in the example.txt file. The delimiter used to separate fields is the space character.
Conclusion
-
The cut command is used for selecting parts of lines or fields from a file.
-
The command has options for selecting specific characters or fields based on their position, as well as a delimiter option for selecting fields based on a custom delimiter.
-
The cut command can be used with other commands to manipulate the output.
-
Remember that the command works on both files and standard input, and the options can be combined to create more complex selections.