Cut Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

cut is a command line utility on Unix and Unix-like operating systems which is used to extract sections from each line of input — usually from a file.

~/codeFactory$ cut --help 
Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes=LIST        select only these bytes
  -c, --characters=LIST   select only these characters
  -d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter
  -f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified
  -n                      (ignored)
      --complement        complement the set of selected bytes, characters
                            or fields
  -s, --only-delimited    do not print lines not containing delimiters
      --output-delimiter=STRING  use STRING as the output delimiter
                            the default is to use the input delimiter
  -z, --zero-terminated    line delimiter is NUL, not newline
      --help     display this help and exit
      --version  output version information and exit

Use one, and only one of -b, -c or -f.  Each LIST is made up of one
range, or many ranges separated by commas.  Selected input is written
in the same order that it is read, and is written exactly once.
Each range is one of:

  N     N'th byte, character or field, counted from 1
  N-    from N'th byte, character or field, to end of line
  N-M   from N'th to M'th (included) byte, character or field
  -M    from first to M'th (included) byte, character or field

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report cut translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/cut>
or available locally via: info '(coreutils) cut invocation'

Using cut with options:

1. -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 an error. Tabs and backspaces are treated like as a character of 1 byte.

~/codeFactory$ cut -b 1,2,3,4 test2.txt 
Nare
Amit
Rahu

~/codeFactory$ cut -b 1-3,5-7 test2.txt 
Narndr
Ami 4
Rahl 2

In this, 1- indicate from 1st byte to end byte of a line
~/codeFactory$ cut -b 1- test2.txt 
Narendra 8
Amit 4
Rahul 20

In this, -5 indicate from 1st byte to 5rd byte of a line
~/codeFactory$ cut -b -5 test2.txt 
Naren
Amit 
Rahul

2. -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.

~/codeFactory$ cut -c 1,2,3,4 test2.txt 
Nare
Amit
Rahu

~/codeFactory$ cut -c 1-4 test2.txt 
Nare
Amit
Rahu

~/codeFactory$ cut -c 1- test2.txt 
Narendra 8
Amit 4
Rahul 20

~/codeFactory$ cut -c -5 test2.txt 
Naren
Amit 
Rahul

3. -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.

~/codeFactory$ cut -f 1 test2.txt 
Narendra 8
Amit 4
Rahul 20

If -d option is used then it considered space as a field separator or delimiter:
~/codeFactory$ cut -d " " -f 2 test2.txt 
8
4
20

Command prints field from first to fourth of each line from the file.
~/codeFactory$ cut -d " " -f 1-2 test2.txt 
Narendra 8
Amit 4
Rahul 20

4. –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.

~/codeFactory$ cut --complement -d " " -f 1 test2.txt 
8
4
20

~/codeFactory$ cut --complement -c 3 test2.txt 
Naendra 8
Amt 4
Raul 20

~/codeFactory$ cut --complement -c 1-5 test2.txt 
dra 8
4
 20

5. –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”.

~/codeFactory$ cut -d " " -f 1,2 --output-delimiter='+' test2.txt 
Narendra+8
Amit+4
Rahul+20

Applications of cut Command

1. How to use cut with pipes(|): 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 names coming from file test2.txt.

~/codeFactory$ cat test2.txt | cut -d " " -f 1 | sort
Amit
Narendra
Rahul

Leave a comment