Cut command in Linux with examples – GeeksforGeeks

Bash cut

The cut command

on UNIX is a command to cut the sections of each line of files and write the result to the standard output. It can be used to cut parts of a line by byte position, character, and field. Basically, the cut command cuts a line and extracts the text. It is necessary to specify the option with the command, otherwise it fails. If more than one file name is provided, the data in each file is not preceded by its file name.



Consider two files that have name state.txt and capital.txt contains 5 names of the Indian states and capitals respectively.

$ cat status.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh

No option specified shows error

. $ cut state.txt Cut: You must specify a list of bytes, characters, or fields Try ‘cut -help’ for more information.

Options and their Description with examples:

1. -b(byte

): To extract the specific bytes, you must follow the -b option with the list of byte numbers separated by comma. The byte range can also be specified using the hyphen(-). It is necessary to specify the list of byte numbers, otherwise it gives error. Tabs and backdrops are treated as a 1-byte character.

List without ranges $ cut -b 1,2,3 state.txt Y Aru Ass Bih Chh List with ranges $ cut -b 1-3,5-7 state.txt Andra Aruach Assm Bihr Chhtti

It uses a special way to select bytes

from the beginning to the end of the line: In this, 1- indicates from the 1st byte to the final byte of a line $cut -b 1- state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh In this, -3 indicates 1st byte to 3rd byte of a line $ cut -b -3 status.txt and Aru Ass Bih Chh 2. -c

(column): To cut by character, use the -c option. This selects the characters given to the -c option. This can be a comma-separated list of numbers or a range of numbers separated by hyphen(-). Eyelashes and backlashes are treated as a character. It is necessary to specify the list of character numbers, otherwise this option fails.


$cut -c [(k)-(n)/(k),(n)/(n)] file name Here, k denotes the starting position of the character and n denotes the final position of the character on each line,

if k and n are separated by “-“, otherwise, they are just the position of the character on each line of the file taken as input.

$ cut -c 2,5,7 status.txt nr rah sm ir hti

The Previous Cut command prints the second, fifth, and seventh characters of each line in the file.

$ cut -c 1-7 state.txt Andhra Arunach Assam Bihar Chhatti The

cut command above prints

the first seven characters of each line in the file.

Cut uses a special shape to select characters

from the beginning to the end of the line: $ cut -c 1- state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh The above command is printed from the first character to the end. Here in the command only the starting position is specified and the end position is omitted. $ cut -c -5 state.txt Andhr Aruna Assam Bihar Chhat The Above command prints the starting position to the fifth character. Here the starting position is omitted and the end position is specified. 3. –

f field: The -c option is useful for fixed-length lines. Most Unix files do not have fixed length lines. To extract the useful information, you must cut by fields instead of columns. The specified field list number must be separated by commas. Ranges are not described with the -f option. cut uses tab as the default field delimiter, but you can also work with another delimiter using the -d option. note: Space is not considered a delimiter in UNIX.


: $cut -d file “delimiter” -f (field number.txt As in the file state.txt fields are space-separated If the -d option is not used, then print the entire line: $ cut -f 1 state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh

If the -d option is used, then the space is

considered as a separator or field delimiter:

$ cut -d ” ” -f 1 state.txt Andhra Arunachal Assam Bihar Chhattisgarh Command prints the field from first to fourth of each line of the file. Command: $ cut -d ” ” -f 1-4 status.txt Departure: Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh

4. -complement: As the name suggests, it complements the output. This option can be used in combination with other options with –f or -c.

$ cut -supplement -d ” ” -f 1 state.txt Pradesh Pradesh Assam Bihar Chhattisgarh $ cut –supplement -c 5 state.txt Andha Pradesh Arunchal Pradesh Assa Biha Chhattisgarh 5. –

output-delimiter: By default the output delimiter is the same as the input delimiter specified in the cut with -d option. To change the output delimiter, use the –output-delimiter=”delimiter” option.

$ cut -d ” ” -f 1,2 state.txt -output-delimiter=’%’ Andhra%Pradesh Arunachal%Pradesh Assam Bihar Chhattisgarh Here the cut

command changes the delimiter (%) in the standard output between the fields that is specified using

the -f option. 6. -version

: This option is used to display the cut version that is currently running on your system. $ cut -version cut

(GNU coreutils) 8.26 Packaged by Cygwin (8.26-2) Copyright (C) 2016 Free Software Foundation, Inc. GPLv3+ License: GNU GPL version 3 or later. This is free software: you are free to change and redistribute it. NO WARRANTY, to the extent permitted by law. Writing by David M. Ihnat, David MacKenzie and Jim Meyering.

Cutting applications Command

1. How to use tail with pipes(|): The cut command can be piped with many other unix commands. In the following example, the output of the cat command is given as input to the cut command with the -f option to sort the state names from the file state.txt in the reverse order.

$ cat state.txt | cut -d ‘ ‘ -f 1 | sort -r Chhattisgarh Bihar Assam Arunachal Andhra

can also be channeled with one or more filters for further processing. As in the following example, we are using the cat, head and cut command and whose output is stored in the list of file names.txt using directive(>).

$ cat state.txt | head -n 3 | cut -d ‘ ‘ -f 1 > list.txt $ cat list .txt Andhra Arunachal Assam

Thank you Saloni Gupta for providing more examples.

Contact US