Updated April 1, 2023
Introduction to Cut command in Linux
Cut command in Linux Shell Scripting is used to cut out the sections of data from each line of files and write the output to another file. Cut command is used to cut sectional parts of a line by position, field, character, and byte. You can use the cut command to extract the required portion of data from a file by slicing it as per the requirement.
Syntax:
cut [option]… [File]
Function of Cut Command in Shell Scripting
To print the selected fields from each input FILE to standard output file
Example: Let us suppose there is a file – training.txt and it has the below data.
cat training.txt
To print the column 3 from a “|” delimited files and to print it in another file, we can use the below syntax by using the “cut” command as below:
Here we are gripping the third column with “|” pipe delimiter and printing the output to another file – output.txt using “>” symbol. This is explained in the below snapshot:
cat training.txt | cut -d “|“ -f3 > output.txt
ls
cat output.txt
Note: When we use the “cut” command, if no file is specified, the cut command in Linux reads the data or file from the standard input.
Options used with Cut Command in Linux
When we use the cut command, we must use one and only one from the following options to get the required output as per our requirement:
Command:
man cut
- -f (–fields=LIST) – To select by specifying a field or a set of fields or a range of fields.
- -b (–bytes=LIST) – To select by specifying a byte or a set of bytes, or range of bytes.
- -c (–characters=LIST) – To select by specifying a character or a set of characters, or range of characters.
You can also list all the options that are available in Linux for the “cut” command by using the man command.
- -d (–delimiter) – Specify a delimiter that may be used rather than the default “TAB” delimiter.
- –complement – complement the choice. Once victimization this feature cut can show all bytes, characters or fields except the chosen.
- -s (–only-delimited) – By default, the cut can print any line that contains no delimiter character. Once victimization this feature cut won’t print lines not containing delimiters.
- –output-delimiter – The default is to use the input delimiter because of the output delimiter. This feature permits you to specify a special output delimiter string.
- –version- output version info and exit
- N – N’th byte or character or field which is counted from 1
- N – from N’th byte or character or field to the end of the line
- N-M – from N’th to M’th byte (including) byte or character or field
- -M – from the first to the M’th (included) byte or character or field
Tag | Description |
-b BYTE-LIST –bytes=BYTE-LIST |
Prints only the bytes in the positions listed in BYTE-LIST. Tabs and backspaces are also treated like other characters and they take up 1 byte. |
-c CHARACTER-LIST –characters=CHARACTER-LIST |
Prints only characters in the positions listed in CHARACTER-LIST. Tabs and backspaces are also treated like other characters and they take up 1 character. |
-f FIELD-LIST –fields=FIELD-LIST |
Prints only the fields listed in the FIELD-LIST. Fields are separated by TAB character by default. |
-n | Do not split the multi-byte characters |
-s –only-delimited |
For ‘-f’, it does not print the lines that do not contain the field separator character in it. |
–output-delimiter=OUTPUT_DELIM_STRING | For ‘-f’, output fields are separated by OUTPUT_DELIM_STRING and by default is to use the input delimiter. |
Examples of Cut command
Options and their brief Description with examples:
1. Field (-f)
“cut” uses tab as by default field delimiter when there is no delimiter specifically specified. To specify a specific delimiter we can use “-d” as an option.
Syntax:
cut -d “delimiter” -f (field number) file_name
Example #1
cat country.txt
Example #2
cat country.txt | cut -d “ “ -f1
Example #3
Command to print the fields from first to the fifth of each line in the file.
cat country.txt | cut -d “ “ -f -4
2. Column (-c)
To cut the data by character, we can use -c option. The list of character numbers is separated by comma (,) or range of numbers separated by (-).
Syntax:
$cut -c [(k)-(n)/(k),(n)/(n)] file_name
Here k denotes the starting position,
N denotes ending position
Example #1
cat country.txt | cut -c 2,5,7
Output:
Example #2
To print the first five characters from each line can be done by the following command:
cat country.txt | cut -c 1-7
Output:
3. Byte (-b)
-b option is used to specify the bytes. The range of bytes can be specified by a hyphen (-). Tabs and backspaces are also treated as each character.
Example #1
cut -b 1,2,3 country.txt
Output:
Example #2
cut -b 1-3,5-7 country.txt
Output:
Example #3
cut -b -3 country.txt
Output:
4. -Complement
The option “-complement” can be used in the combination of other options with -f or -c.
Example #1
cut –complement -d ” ” -f 1 country.txt
Output:
Example #2
cut –complement -c 5 country.txt
Output:
5. -output-delimiter
By default, the output delimiter is the same as the input delimiter that we mention in the cut command with -d option. To change the delimiter in the output result, we can use -output-delimiter=”required_delimiter”
Example #1
cut -d ” ” -f 1,2 country.txt –output-delimiter=’*’
Output:
6. To view the 10 most recent used commands in Linux
Below syntax is used to see the recent most 10 commands that are used in Linux for that particular instance of the server. To do so, we need to follow the below syntax.
Here uniq denotes the unique commands used recently,
sort –r denotes sorting of data in reverse order and head denotes the starting of the file.
history | cut -c8- | sort | uniq -c | sort -rn | head
Output:
Conclusion
By now you should have good knowledge of how to use the Linux “cut” command. But, the cut command also has a few limitations like it doesn’t support specifying more than one character as a delimiter and it doesn’t support multiple delimiters.
Recommended Articles
This is a guide to Cut Command in Linux. Here we discuss the Functions and Options used with Cut Command in Linux along with the Examples. You may also have a look at the following articles to learn more –