Friday, 2 August 2019

Linux - CUT Command

The cut command is used to used to display selected columns or fields from each line of a file.
It will be most useful when we are working with shell scripting programs.

Syntax:
cut options filename

We are taking /etc/passwd file for doing some particular tasks with cut command. /etc/passwd file stores the userid details.

Example-1:
To display characters from each line:
Cut command can take out the characters in a line based on their position. We can use “-c” option for this,
Syntax:
cut -c positions-range file-name

For example,
we want to display 3rd and 4th characters of /etc/passwd file.
We can use comma separator for giving multiple character display. Sample command and output is,
cut -c 3,4 /etc/passwd

[root@sys2 ~]# cut -c 3,4 /etc/passwd
ot
n:
em
m:
:x

We want to display character from 10 to 20 from file:
To print the range of characters of a line in a file,
cut -c 10-20 /etc/passwd

[root@sys2 ~]# cut -c 10-20 /etc/passwd
0:root:/roo
:bin:/bin:/
2:2:daemon:
:adm:/var/a
lp:/var/spo
0:sync:/sbi
x:6:0:shutd
0:halt:/sbi
12:mail:/va
:14:uucp:/v
x:11:0:oper

To Display first 10 character of a /etc/passwd:
cut -c -10 /etc/passwd

[root@sys2 ~]# cut -c -10 /etc/passwd
root:x:0:0
bin:x:1:1:
daemon:x:2
adm:x:3:4:
lp:x:4:7:l
sync:x:5:0
shutdown:x
halt:x:7:0
mail:x:8:1

To Display characters from 10th characters onwards:
cut -c 10- /etc/passwd

[root@sys2 ~]# cut -c 10- /etc/passwd
0:root:/root:/bin/bash
:bin:/bin:/sbin/nologin
2:2:daemon:/sbin:/sbin/nologin
:adm:/var/adm:/sbin/nologin
lp:/var/spool/lpd:/sbin/nologin
0:sync:/sbin:/bin/sync
x:6:0:shutdown:/sbin:/sbin/shutdown
0:halt:/sbin:/sbin/halt
12:mail:/var/spool/mail:/sbin/nologin
:14:uucp:/var/spool/uucp:/sbin/nologin

To display fields based on delimiters:
For separating the columns based on delimiters we can use option “-d” with cut command.
We want to display 3rd field from each line of file with delimiter as “:”
Field numbers in a file are counted from 0 onwards. So 3rd field is generally referred as “-f2” option.

Sample command output is given below,
[root@sys2 ~]# cut -d':' -f2 /etc/passwd
x
x
x
x
x
x
x

Verify the field using cat /etc/passwd

Display fields from 1 to 4 from file:
cut -d':' -f1-4 /etc/passwd

[root@sys2 ~]# cut -d':' -f1-4 /etc/passwd
root:x:0:0
bin:x:1:1
daemon:x:2:2
adm:x:3:4
lp:x:4:7
sync:x:5:0
shutdown:x:6:0
halt:x:7:0
mail:x:8:12
uucp:x:10:14

Display first 5 fields of a file with delimiter as “:”:-
cut -d':' -f-5 /etc/passwd

[root@sys2 ~]# cut -d':' -f-5 /etc/passwd
root:x:0:0:root
bin:x:1:1:bin
daemon:x:2:2:daemon
adm:x:3:4:adm
lp:x:4:7:lp
sync:x:5:0:sync
shutdown:x:6:0:shutdown
halt:x:7:0:halt
mail:x:8:12:mail
uucp:x:10:14:uucp
operator:x:11:0:operator

To display piece of any command output:
Cut command can take input from another command output rather than a file itself. Below given an example of “who am I” command output is passed to cut command as input and seeing the first field of it.

Let’s look at the command output in general,
[root@sys2 ~]# who am i
root     pts/0        
2016-03-17 15:00 (192.168.1.26)

Now let’s cut the first field of this command output based on ‘ ’ as delimiter.
who am i | cut -f1 -d' '

Command output,
[root@sys2 ~]# who am i | cut -f1 -d' '
root

0 comments:

Post a Comment