How to use the command 'cut' (with examples)
The ‘cut’ command is a versatile utility that allows users to extract specific fields or characters from a file or standard input. It is commonly used to manipulate delimited text files by extracting specific columns or fields. The command also supports various options to customize the extraction process.
Use case 1: Print a specific character/field range of each line
Code:
command | cut --characters 1|1,10|1-10|1-|-10
Motivation: When working with large datasets or log files, it is often necessary to extract only specific characters or fields from each line. The ‘cut’ command provides an easy way to achieve this requirement.
Explanation:
--characters
: Specifies the characters to be extracted from each line. In the example, we are extracting the first character, characters 1 and 10, characters 1 to 10, and characters 1 to the end and the last 10 characters from each line.1|1,10|1-10|1-|-10
: These arguments specify the range of characters to be extracted. ‘|’ is used to separate multiple ranges.
Example output:
Input: Hello,World!
Output: H,HW!Hello,World!HHello,World!Hello,World
Input: This-is-an-example
Output: T,Tn-elshowihs-an-an,,’anl”pme-sexas
Input: 1234567890
Output: 1,19,1234567890,=123456780,123456789
Use case 2: Print a range of each line with a specific delimiter
Code:
command | cut --delimiter="," --fields=1
Motivation: Delimited files are commonly used in data processing tasks. Extracting specific columns from each line based on a delimiter is a common requirement. The ‘cut’ command simplifies this task by providing a convenient way to specify the delimiter and fields to be extracted.
Explanation:
--delimiter
: Specifies the delimiter used in the input file. In the example, we are using a comma (’,’) as the delimiter.--fields
: Specifies the range of fields to be extracted. In the example, we are extracting the first field from each line.
Example output:
Input: John,Doe,1980,USA
Output: John
Input: Alice,Brown,1995,UK
Output: Alice
Use case 3: Print a range of each line of the specific file
Code:
cut --characters=1 path/to/file
Motivation: The ‘cut’ command can also be used directly on specific files, eliminating the need for a preceding command. This is useful when it is necessary to process a file without piping the output from another command.
Explanation:
--characters
: Specifies the characters to be extracted from each line. In the example, we are extracting the first character from each line.path/to/file
: Specifies the path to the file to be processed.
Example output:
Input file:
This is line 1.
This is line 2.
This is line 3.
Output:
T
T
T
Conclusion:
The ‘cut’ command is a useful tool for manipulating and extracting specific characters or fields from files or standard input. Its simplicity and versatility make it an essential part of any developer or data analyst’s toolkit. With the ability to customize field delimiters and ranges, the ‘cut’ command provides an efficient solution for various text processing tasks.