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 Command
The basic syntax of the
cut command is:
cut OPTION... [FILE]...
`OPTION` specifies the desired behavior
` represents the input file.
FILE is not specified, `
cut` reads from standard input (stdin).
Options Available in cut Command
Here is a list of the most commonly used options with the `
Selects only the bytes specified in
Selects only the characters specified in
DELIM as the field delimiter character instead of the tab character.
Selects only the fields specified in
LIST, separated by the delimiter character (default is tab).
Do not split multi-byte characters (no effect unless
-c is specified).
Invert the selection of fields/characters. Print the fields/characters not selected.
Practical Examples of cut Command
Let us consider two files having name state.txt and capital.txt contains 5 names of the Indian states and capitals respectively.
$ cat state.txt
Without any option specified it displays error.
$ cut state.txt
cut: you must specify a list of bytes, characters, or fields
Try 'cut --help' for more information.
Extract Specific Bytes (
-b) Using cut Command
-b(byte): To extract the specific bytes, you need to follow -b option with the list of byte numbers separated by comma. Range of bytes can also be specified using the hyphen(-). It is necessary to specify list of byte numbers otherwise it gives error.
Tabs and backspaces are treated like as a character of 1 byte.
List without ranges:
cut -b 1,2,3 state.txt
list without range
List with ranges:
cut -b 1-3,5-7 state.txt
list 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 line
In this, 1- indicate from 1st byte to end byte of a line
cut -b 1- state.txt
special form with -b option
In this, -3 indicate from 1st byte to 3rd byte of a line
cut -b -3 state.txt
special form -b option
Cut by Character (
-c) Using cut Command
-c (column): To cut by character use the -c option. This selects the characters given to the -c option. This can be a list of numbers separated comma or a range of numbers separated by hyphen(-).
Tabs and backspaces are treated as a character. It is necessary to specify list of character numbers otherwise it gives error with this option.
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.
Extract specific characters:
cut -c 2,5,7 state.txt
Extract specific characters
Above cut command prints second, fifth and seventh character from each line of the file.
Extract first seven characters:
cut -c 1-7 state.txt
Extract 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 line
cut -c 1- state.txt
selecting 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.txt
selecting 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): -c option is useful for fixed-length lines. Most unix files doesn’t have fixed-length lines. To extract the useful information you need to cut by fields rather than columns. List of the fields number specified must be separated by comma. Ranges are not described with -f option. cut uses tab as a default field delimiter but can also work with other delimiter by using -d option.
Note: Space is not considered as delimiter in UNIX.
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.txt
Extract 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.txt
space as a field separator or delimiter
Extract fields 1 to 4:
Command prints field from first to fourth of each line from the file.
cut -d " " -f 1-4 state.txt
Command 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
cut --complement -c 5 state.txt
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.
display version of cut command
How to use tail with pipes(|) in cut Command
The cut command can be piped with many other commands of the unix. 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 -r
using 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
redirecting output in different file
Frequently Asked Questions on cut Command in Linux – FAQs
How do I use the
cut command to extract specific columns from a file?
Example: To extract the first and third columns from a CSV file named `
cut -d',' -f1,3 data.csv
Can I use
cut to extract a range of characters from each line?
Yes, you can. To extract characters 5 to 10 from each line of a file named
cut -c5-10 text.txt
How can I change the delimiter used by the
Use the `
-d` option followed by the delimiter character. For example, to use a colon (
:) as the delimiter.
cut -d':' -f1,3 data.txt
Is it possible to use
cut to extract fields based on character position?
Yes, you can specify character positions with the `
-c` option. For example, to extract characters 1 to 5 and 10 to 15 from each line.
cut -c1-5,10-15 data.txt
How do I use
cut to extract fields based on a specific delimiter and store them in a new file?
To extract fields separated by commas and store them in a new file named `
cut -d',' -f1,3 data.csv > output.txt
In this article we discussed the `
cut` command in Linux which is a versatile tool for extracting specific sections from files based on byte position, character, or field. It slices lines of text and outputs the extracted data. Failure to specify an option with the
cut command results in an error. Multiple files can be processed, but the output does not include the file names. Options such as `
-c`, and `
-f` allow extraction by byte, character, and field, respectively. The
--complement option inverts the selection, printing what is not selected, and
--output-delimiter changes the output delimiter. The command also includes options for version display and can be used in combination with other commands through pipes for additional processing.
Share your thoughts in the comments
Please Login to comment...