Home » linux-commands » Useful examples of cut command in Linux

Useful examples of cut command in Linux

In this article you will learn how to use cut command on linux system. In computing, cut is a Linux/Unix command line function which is generally used to pull out sections from each line of input.
Cut command path.

root@linuxpcfix [~]#which cut
/bin/cut
cut command options
-b select only bytes from line
-c select characters from line
-f select field
-d use character DELIMITER instead of a tab for the field delimiter.
-s, –only-delimited do not print lines not containing delimiters.
#cut [option] file name

root@linuxpcfix [~]#cat test.txt

this is my first page
is this my first page
page is first

If you want to cut from third character to end of the line then use the following command.

root@linuxpcfix [~]#cut -c3- test.txt

is is my first page
this my first page
ge is first

To select the fourth field of each line execute the following command

root@linuxpcfix [~]# cut -f 4 test.txt

first
first

To select the third field

root@linuxpcfix [~]# cut -f 3 test.txt

my
my
first

root@linuxpcfix [~]# cat testfile.txt

this is my first command hellow world colour ram bob name karen
asshish charles movie picture SATA TRP linux pc fix dot com
unix support dot co in india pak test page

To select from third field to end of the line.

root@linuxpcfix [~]# cut -f 3- testfile.txt

my first command hellow world colour ram bob name karen
movie picture SATA TRP linux pc fix dot com
dott coo inn india pak test page

Select the field from 3 to 9 use the below command.

root@linuxpcfix [~]# cut -f 3-9 testfile.txt

my first command hellow world colour
movie picture SATA TRP linux
dott coo inn india pak

Select the field 1 and 3 use the below command.

root@linuxpcfix [~]# cut -f 1,3 testfile.txt

this my
asshish
unix

If field are separated by space then use the following command.

root@linuxpcfix [~]# cut -f 3 -d ‘press space’ n.txt

first
father
mother
son

If field are separated by : then use the following command

#cat /etc/passwd

user:x:502:502:user:/home/user:/bin/bash
user1:x:503:503:user1:/home/user1:/bin/bash
user2:x:504:504:user2:/home/user2:/bin/bash
user3:x:505:505:user3:/home/user3:/bin/bash
user4:x:506:506:user4:/home/user4:/bin/bash

To select first field execute the below command

root@linuxpcfix [~]#cut -f 1 -d ‘:’ /etc/passwd

user1
user2
user3
user4

To select sixth field execute the below command

root@linuxpcfix [~]#cut -f 6 -d ‘:’ /etc/passwd

/user
/home/user1
/home/user2
/home/user3
/home/user4

To select first and seventh field execute the below command

root@linuxpcfix [~]#cut -f 1,7 -d ‘:’ /etc/passwd

user:/bin/bash
user1:/bin/bash
user2:/bin/bash
user3:/bin/bash
user4:/bin/bash

root@linuxpcfix [~]#cat example.txt

Mathew,Father,40,Manager
Diana,Mother,35,Housewife
John,Son,10,student

To get the name, relationship and age, excluding the profession as below

root@linuxpcfix [~]#cut -d, -f 1-3 example.txt

Mathew,Father,40
Diana,Mother,35
John,Son,10

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry from last 7 years.

Leave a Reply

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

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud