Linux cut Command
The cut command in Linux is used to extract sections of text from files or standard input. It is particularly useful for processing delimited data.
Syntax
cut [OPTION]... [FILE]...
Options
| Option | Description |
|---|---|
-b |
Select only the bytes specified. |
-c |
Select only the characters specified. |
-d |
Specify a delimiter (default is tab). |
-f |
Select only the fields specified (requires -d). |
--complement |
Complement the selection (select everything except the specified). |
--output-delimiter |
Specify the output delimiter (default is the input delimiter). |
--help |
Display help information. |
--version |
Display version information. |
Examples
1. Extract specific fields from a file
cut -d',' -f1,3 file.txt
This command extracts the 1st and 3rd fields from file.txt, assuming fields are separated by commas.
2. Extract specific characters
cut -c1-5 file.txt
This command extracts the first 5 characters from each line of file.txt.
3. Extract specific bytes
cut -b1-10 file.txt
This command extracts the first 10 bytes from each line of file.txt.
4. Change the output delimiter
cut -d',' -f1,2 --output-delimiter='|' file.txt
This command extracts the 1st and 2nd fields from file.txt and separates them with a pipe (|) in the output.
Conclusion
The cut command is a powerful tool for text processing in Linux. By mastering its options, you can efficiently extract and manipulate data from files or standard input.