Linux Cut Command with Practical Examples

linux cut command

The cut command is used in Linux and Unix systems to cut parts and sections from each line of a file and write the result to standard output. It can be used to cut parts of a line by byte position, character and field (delimiter).

In this tutorial, we learn Linux cut command with some practical examples you can use in your day to day command line activities.

Cut Command and Syntax

The basic syntax for the cut command is as follows:

cut OPTION... [FILE]...

Let's check cut options and without any option cut command won't work.

Cut Options

-f  : Extract by specifying a field. Cut command uses 'TAB' as the default field delimiter.

-d : 'Tab' is the default delimiter and using this option you can use specific delimiter.

-b : To extract by specifying a byte. The range of bytes can also be specified.

-c : To cut by character. This can be a list of numbers separated comma or a range of numbers separated by a hyphen (-).

–complement : This will complement the selection

–output-delimiter : To change the output delimiter use the option -output-delimiter='delimiter'.

--only-delimited : Cut will not print lines not containing delimiters

We will use the following text file named 'content.txt' and /etc/passwd file throughout this tutorial to illustrate our examples.

$ cat content.txt 
Ubuntu Linux
Microsoft Windows
OsX El Capitan
Unix
FreeBSD

How to cut by delimiter

The most commonly used cut option is a combination of -d and -f. It will basically extract content based on specific delimiter and listed fields.

For example the following prints only the 1st field of each line from '/etc/passwd' file using the delimiter (:).

$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
...

The following example where we use space (" ") as delimiter and cut the 1st field from the file named 'content.txt'.

$ cut -d " " -f 1 content.txt 
Ubuntu
Microsoft
OsX
Unix
FreeBSD

This example, extracts more than one field from a specific file. Here we extract 1st and 6th field using colon (:) delimiter from the file '/etc/passwd' which has the string '/bin/bash':

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
slax:/home/slax

To display the range of fields, specify start field and end field separated by hypen (-) as shown below:

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7
root:x:0:0:/root:/bin/bash
slax:x:1000:1000:/home/slax:/bin/bash

How to complement the output selection

To complement the selection field list use --complement option. This option used where to select all fields except the specified fields.

In the following example command prints all the fields except the 2nd field in the from '/etc/passwd' file:

$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -f2
root:0:0:root:/root:/bin/bash

How to specify an output delimiter

To specify the output delimiter use the --output-delimiter option. Input delimiter is specified by -d option and by default output delimiter is the same as input delimiter.

Lets first check out the output without using output delimiter as follows:

$  cut -d: -f1,7  /etc/passwd |  sort |  uniq -u
_apt:/usr/sbin/nologin
backup:/usr/sbin/nologin
bin:/usr/sbin/nologin
daemon:/usr/sbin/nologin
dnsmasq:/usr/sbin/nologin
games:/usr/sbin/nologin
gnats:/usr/sbin/nologin
irc:/usr/sbin/nologin
landscape:/usr/sbin/nologin
list:/usr/sbin/nologin
lp:/usr/sbin/nologin
lxd:/bin/false

Now I have added --output-delimiter option and input delimiter colon (:) is replaced with output delimiter 'SPACE" as follows:

$  cut -d: -f1,7 --output-delimiter ' ' /etc/passwd |  sort |  uniq -u
_apt /usr/sbin/nologin
backup /usr/sbin/nologin
bin /usr/sbin/nologin
daemon /usr/sbin/nologin
dnsmasq /usr/sbin/nologin
games /usr/sbin/nologin
gnats /usr/sbin/nologin
irc /usr/sbin/nologin
landscape /usr/sbin/nologin
list /usr/sbin/nologin
lp /usr/sbin/nologin
lxd /bin/false

Let's check another example, here we use output delimiter to print on each field in a new line.

Here we use --output-delimiter as $’\n’ which indicates a new line.

Check the output:

$ grep root /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n'
root
/root
/bin/bash
operator
/root
/sbin/nologin

How to cut by characters

The -c (column) option is used for cutting by character position. Remember that 'TABS" and 'Spaces' are also treated as characters.

To print 1st character from each line from the file named content.txt, use below:

$ cut -c 1 content.txt
U
M
O
U
F

In the following example we will display character 1 through 7 (range) of each line from the file:

$ cut -c 1-7 content.txt
Ubuntu
Microso
OsX El
Unix
FreeBSD

Let's check how to select columns by either specific start or end position.

To extract columns from 2nd character to last character:

$ cut -c2- content.txt
buntu Linux
icrosoft Windows
sX El Capitan
nix
reeBSD

To extract columns from 1st character to 4th character:

cut -c-4 content.txt
Ubun
Micr
OsX
Unix
Free

How to cut by bytes

Use the -b option to select out a portion of a line by specifying a byte position by numbers separated by comma (,). Using hyphen you can specify a range of bytes.

The following example cut from 1st, 2nd and 3rd bytes from the file named 'content.txt':

$ cut -b 1,2,3 content.txt 
Ubu
Mic
OsX
Uni
Fre

We can also list with ranges using the below command:

$ cut -b 1-3,5-7 content.txt 
Ubutu 
Micoso
OsXEl 
Uni
FreBSD

Some practical cut examples

Cut is most practical used with a combination of different Linux or Unix commands.

For example, you want to extract 'USER', 'PID' and "COMMAND" using ps command:

 ps -L u n | tr -s " " | cut -d " " -f 2,3,14-
USER PID COMMAND
0 676 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
0 681 /sbin/agetty -o -p -- \u --noclear tty1 linux
0 23174 -bash
0 26737 ps -L u n
0 26738 tr -s
0 26739 cut -d -f 2,3,14-

Let's take another example to extract 'total', 'used' and 'free' value of memory and save to a text file using multiple command:

$ free -m | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2-4 >> memory.txt
Output
$ cat memory.txt
985 86 234

Conclusion

The cut command can be piped with many other Linux or Unix commands. It can be piped with one or more filters for additional text processing.

One of the limitations of cut command is it doesn't support specifying more than one character as a delimiter. Multiple blanks are counted as multiple field separators and have to use tr command before cut to get the desired output.

Slavisa Milojkovic 3:39 am

Comments

Your email address will not be published. Required fields are marked *