Showing posts with label Linux cut. Show all posts
Showing posts with label Linux cut. Show all posts

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

Wednesday, 31 July 2019

Linux - cut command useful examples

cut is a command from coreutility which allows you to print selected parts of lines from each FILE to standard output.
Here is an output from lscpu -p option, it will be used in examples below as testfile
# Socket,CPU,Core,Address,Online,Configured,Polarization
0,0,0,,Y,,
0,1,1,,Y,,
0,2,2,,Y,,
0,3,3,,Y,,
0,4,4,,Y,,
0,5,5,,Y,,
1,6,6,,Y,,
1,7,7,,Y,,
1,8,8,,Y,,
1,9,9,,Y,,
1,10,10,,Y,,
1,11,11,,Y,,

Example 1: print one specified field(second field in below example)

# cut -d',' -f2 testfile
CPU
0
1
2
3
...
Option -d and -f
       -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

Example 2: print two fields

# cut -d',' -f2,5 testfile
CPU,Online
0,Y
1,Y
2,Y
...

Example 3: print a range of fields

# cut -d',' -f2-5 testfile
CPU,Core,Address,Online
0,0,,Y
1,1,,Y
...

Example 4: print a range of fields, from specified field to rest

# cut -d',' -f2- testfile
CPU,Core,Address,Online,Configured,Polarization
0,0,,Y,,
1,1,,Y,,
...

Example 5: print a range of fields, the first to the specified one

# cut -d',' -f-2 testfile
# Socket,CPU
0,0
0,1
...

Same rule applys to -b and -c

-b and -c
       -b, --bytes=LIST
              select only these bytes
       -c, --characters=LIST
              select only these characters
# cut -c1 testfile
#
0
...

# cut -c1,5 testfile
#c
00
...

# cut -c1-5 testfile
# Soc
0,0,0
...

# cut -c-5 testfile
# Soc
0,0,0
...
# cut -c5- testfile
cket,CPU,Core,Address,Online,Configured,Polarization
0,,Y,,
1,,Y,,
...

More options

       -n     with -b: don't split multi byte characters
       --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