# Cut command

It cuts any records using characters and fields from file or from any command output.

Syntax: cut &lt;options&gt; &lt;filename&gt;

There are various options used in cut command.

a) -c: cut by characters

b) -f : cut by fields but we cannot use this option without defining the delimeter symbol. Delimeter indicates this is my first field, this is my second field and so. It will specify with the help of symbol.

c) -d : define the delimeter symbol. It can be anything like

Note:- -f and -d works simultaneously.

Eg: Cut 1st character from /etc/passwd file.

`cut -c1 /etc/passwd`

Eg: Cut 1st and 3rd character from /etc/passwd file.

`cut -c1,3 /etc/passwd`

Eg: Print 5 characters from /etc/passwd file

`cut -c1-5 /etc/passwd`

Eg: If you want to print 1st field and you are not using -d flag then it will print the whole output

`ubuntu@ip-172-31-25-118:~$ tail -n 3 /etc/passwd | cut -f1 jethalal:x:1001:1001::/home/jethalal:/bin/sh`

`daya:x:1002:1003::/home/daya:/bin/sh`

`iyer:x:1003:1004::/home/iyer:/bin/sh`

Eg: On the basis of ‘:’, print 1st column

`ubuntu@ip-172-31-25-118:~$ tail -n 3 /etc/passwd | cut -d: -f1`

`jethalal`

`daya`

`iyer`

If you want to give space as a delimeter then use `cut -d' ' -f1-4 <filename>`

There are some drawbacks used by cut command. Hence we need to use AWK command. AWK command is an advance version.

Eg: `ubuntu@ip-172-31-25-118:~$ df -h`

`Filesystem Size Used Avail Use% Mounted on`

`/dev/root 6.8G 2.2G 4.5G 33% /`

`tmpfs 458M 0 458M 0% /dev/shm`

`tmpfs 183M 876K 182M 1% /run tmpfs 5.0M 0 5.0M 0% /run/lock`

As there are spaces which is increasing and decreasing between FileSystem and Size Column. So you can print the 1st column using cut command because before every column it has a space.

`ubuntu@ip-172-31-25-118:~$ df -h | cut -d' ' -f1`

`Filesystem`

`/dev/root`

`tmpfs`

`tmpfs`

As I have mentioned there is an issue with space increasing/decreasing between 1st and 2nd column. Now I want to print 2nd column.

This command will not work.

`ubuntu@ip-172-31-25-118:~$ df -h | cut -d' ' -f2`

Therefore we are using AWK command. In AWK command, no need to define space as a delimeter. Here Space is a default delimeter. It will define the space as a $ symbol.

Eg: Now I want 1st and 2nd column from df -h command

`ubuntu@ip-172-31-25-118:~$ df -h | awk '{print $1,$2}'`

`Filesystem Size`

`/dev/root 6.8G`

`tmpfs 458M`

`tmpfs 183M`

Here output is not sequence, then run below command

`ubuntu@ip-172-31-25-118:~$ df -h | awk '{print $1,$2}' | column -t`

You can see the difference in your output when you run this command

Eg: You can define delimeter using -F in awk command.

`awk -F’:’ ‘{print $1} <filename>`
