The cut command in UNIX 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(-b), character(-c) and field(-f). Basically the cut command slices a line and extracts the text. It is necessary to specify options with commands otherwise it gives errors. If more than one file name is provided then data from each file is not preceded by its file name. 


SYNOPSIS

       cut OPTION... [FILE]...


Let’s consider /etc/passwd file for demonstrations,


  • -b(byte): To extract the specific bytes.


List without ranges 


[oracle@srv1 ~]$ cut -b 1,2,3 /etc/passwd

roo

bin

dae

adm

lp:

syn

shu

hal

mai

ope


List with ranges


[oracle@srv1 ~]$ cut -b 1-3,5-7 /etc/passwd 

roo:x:

binx:1

daeon:

admx:3

lp::4:

syn:x:

shudow

hal:x:

mai:x:

opeato


  • -c (column): To cut by character use the -c option.


List without ranges


[oracle@srv1 ~]$ cut -c 2,5,7 /etc/passwd

o::

ix1

ao:

dx3

p::

y::

hdw

a::

a::

pao


List with ranges


[oracle@srv1 ~]$ cut -c 1-7 /etc/passwd

root:x:

bin:x:1

daemon:

adm:x:3

lp:x:4:

sync:x:

shutdow

halt:x:

mail:x:

operato


When want to show first 5 characters,


[oracle@srv1 ~]$ cut -c -5 /etc/passwd

root:

bin:x

daemo

adm:x

lp:x:

sync:

shutd

halt:

mail:

opera


  • -f (field): -c option is useful for fixed-length lines.


One example is as below. Use this type with a delimiter,


[oracle@srv1 ~]$ cut -d ':' -f 1 /etc/passwd

root

bin

daemon

adm

lp

sync

shutdown

halt

mail