The cut command in linux is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character, and field. The cut command slices a line and extracts the text. It is necessary to specify an option with a command otherwise it gives an error. If more than one file name is provided then data from each file is not preceded by its file name.
Syntax of cut CommandThe basic syntax of the cut command in Linux is:
cut OPTION... [FILE]..
`OPTION`
defines how the Linux cut command behaves.FILE
`
is the input file. If no file is specified, the cut command Unix reads from standard input (stdin).Options Available in cut CommandNote: If
FILE
is not specified, `cut`
reads from standard input (stdin).
Here is a list of the most commonly used options with the Linux cut command:
Option
Description
-b, --bytes=LIST
Selects only the bytes specified in LIST
(e.g., -b 1-3,7
).
-c, --characters=LIST
Selects only the characters specified in LIST
(e.g., -c 1-3,7
).
-d, --delimiter=DELIM
Uses DELIM
as the field delimiter character instead of the tab character.
-f, --fields=LIS
Selects only the fields specified in LIST
, separated by the delimiter character (default is tab).
-n
Do not split multi-byte characters (no effect unless -b
or -c
is specified).
--complement
Invert the selection of fields/characters. Print the fields/characters not selected.
--output-delimiter
Changes the output delimiter for fields in the cut command bash.
Practical Examples of cut CommandLet us consider two files having name state.txt and capital.txt contains 5 names of the Indian states and capitals respectively.
$ cat state.txtsee file content
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Running the cut command without an option results in an error:
$ cut state.txtcut command Extract Specific Bytes (
-b
) Using cut Command
-b(byte): The -b option in the Linux cut command extracts specific bytes. You specify byte numbers separated by commas or a range with a hyphen (-). Without a valid byte list, the cut command errors out.
List without ranges:Note: Tabs and backspaces are treated like as a character of 1 byte.
It will extract and display the 1st, 2nd, and 3rd bytes (characters) from each line of the state.txt
file
cut -b 1,2,3 state.txtlist without range List with ranges:
It will extract characters from positions 1 to 3, and then from 5 to 7, from each line of the file named state.txt
cut -b 1-3,5-7 state.txtlist with range
It uses a special form for selecting bytes from beginning upto the end of the line:
Special Form: Selecting bytes from beginning to end of lineIn this, 1- indicate from 1st byte to end byte of a line
cut -b 1- state.txtspecial form with -b option
In this, -3 indicate from 1st byte to 3rd byte of a line
cut -b -3 state.txtspecial form -b option Cut by Character (
-c
) Using cut Command
-c (column): The -c option in the cut command Unix extracts characters. You can specify a list of character positions with commas or a range with a hyphen (-).
Tabs and backspaces are treated as single characters, and a valid character list is required to avoid errors.
Syntax:
cut -c [(k)-(n)/(k),(n)/(n)] filename
Here,k denotes the starting position of the character and n denotes the ending position of the character in each line, if k and n are separated by "-" otherwise they are only the position of character in each line from the file taken as an input.
It will extract and display the characters at positions 2, 5, and 7 from each line of the file named state.txt
cut -c 2,5,7 state.txtExtract specific characters
This cut in Unix command prints the 2nd, 5th, and 7th characters from each line.
It extracts and displays the first 7 characters from each line of the file named state.txt
.
cut -c 1-7 state.txtExtract first seven characters
Above cut command prints first seven characters of each line from the file. Cut uses a special form for selecting characters from beginning upto the end of the line:
Special Form: Selecting characters from beginning to end of lineIt extracts and displays the first character of each line from the file named state.txt
.
cut -c 1- state.txtselecting characters from beginning to end of line using -c option
Above command prints starting from first character to end. Here in command only starting position is specified and the ending position is omitted.
cut -c -5 state.txtselecting characters from beginning to end of line using -c option
Above command prints starting position to the fifth character. Here the starting position is omitted and the ending position is specified.
Cut by Field (-f
) Using cut Command
-f (field): The -f option in the cut command Linux is ideal for files without fixed-length lines. It extracts fields separated by a delimiter (default is tab). Use the Linux "cut -d" option to specify a custom delimiter, like a space.
Note: Space is not considered as delimiter in UNIX.
Syntax:
cut -d "delimiter" -f (field number) file.txt
Extract first field :
Like in the file state.txt fields are separated by space if -d option is not used then it prints whole line:
cut -f 1 state.txtExtract first field using -f option
If `-d` option is used then it considered space as a field separator or delimiter:
cut -d " " -f 1 state.txtspace as a field separator or delimiter
Command prints field from first to fourth of each line from the file.
cut -d " " -f 1-4 state.txtCommand prints field from first to fourth Complement Output (
--complement
) Using cut Command
--complement: As the name suggests it complement the output. This option can be used in the combination with other options either with -f or with -c.
cut --complement -d " " -f 1 state.txt--complement
It removes the 5th character from each line of state.txt
and prints the rest of the line.emoves the 5th character from each line of state.txt and prints the rest of the line.
cut --complement -c 5 state.txt--complement Output Delimiter (
--output-delimiter
) Using cut Command
--output-delimiter: By default the output delimiter is same as input delimiter that we specify in the cut with -d option. To change the output delimiter use the option --output-delimiter="delimiter".
cut -d " " -f 1,2 state.txt --output-delimiter='%'
Here cut command changes delimiter(%) in the standard output between the fields which is specified by using -f option .
Display Version (--version
) Using cut Command
--version: This option is used to display the version of cut which is currently running on your system.
cut --versiondisplay version of cut command How to use tail with pipes(|) in cut Command
The cut command in unix can be piped with many other commands. In the following example output of the cat command is given as input to the cut command with -f option to sort the state names coming from file state.txt in the reverse order.
cat state.txt | cut -d ' ' -f 1 | sort -rusing tail with pipe (|) in cut command
It can also be piped with one or more filters for additional processing. Like in the following example, we are using cat, head and cut command and whose output is stored in the file name list.txt using directive(>).
cat state.txt | head -n 3 | cut -d ' ' -f 1 > list.txt
cat list.txtredirecting output in different file Conclusion
Linux's cut command is a basic yet useful command for cutting out from a file or input. It has options such as -b, -c, and -f that allow it to cut data on the basis of bytes, characters, or fields, and is very convenient for extracting just what you require. With the -d option in the bash cut command, you can tailor delimiters, and with --complement or --output-delimiter, you can detail your output. The cut command Unix also plays well with pipes, so you can couple it with other commands such as cat or sort for advanced tasks.
cut command & Splitting a file vertically in Linux
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4