8 cut command – using with shell scripting practical examples

8 cut command is used to process the text. You can use this command to extract portion of text from a file by selecting columns.  The cut utility is a great of the UNIX / Linux philosophy of “do one thing and do it well”. cut just cuts lines of test, base on a (single character) delimiter.

There are two basic forms in which cut is generally used:

for this article I am using the sample text file for demo.

[root@NagiosServer temp]# cat test.txt
Tech tutorials is a website where you can learn lot og things like
computer hardware, computer networking: Linux; SAN, NAS, DAS
so many certifications and gaining knowledge freely

1. Print particular character cut command

[root@NagiosServer temp]# cat test.txt |cut -c 1
T
c
s

see the above example, as I used to -c option to print the particular character from all the lines. -c 1 means it will print first character of all the lines in the file.

-c option will specify the characters.

2. Printing range of characters

[root@NagiosServer temp]# cat test.txt |cut -c1-5
Tech
compu
so ma

We can also cut the number of characters using range like 1-5. See the above example printing 1 to 5 number of characters.

3. Print except given range of characters

This below example is tricky, I would like to print an lines without given range of characters from Start. See example below.

[root@NagiosServer temp]# cut -c5- test.txt
 tutorials is a website where you can learn lot og things like
uter hardware, computer networking: Linux; SAN, NAS, DAS
any certifications and againing knowledge freely

in above command it is printed all the lines and excluded 1to5 characters from start of the lines.

4. Select Specific Filed from a file

To print the specific fields using cut command we have to use -f and -d options. Using -f option we can specify the field number to print. -d option we can specify delimiter.

The following example displays only first field of each lines from /etc/passwd file using the field delimiter : (colon). In this case, the 1st field is the username. The file

[root@NagiosServer temp]# cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail

5. Select multiple fields from a file

You can also print multiple fields from a file. see the example below

[root@NagiosServer temp]# cat /etc/passwd |grep root  <<-----Original file content.
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@NagiosServer temp]# cat /etc/passwd |grep root |cut -d':' -f1,6  <<--- After printing field1 and 6.
root:/root
operator:/root
[root@NagiosServer temp]# cat /etc/passwd |grep root |cut -d':' -f1,3 <<--- After printing field1 and 3
root:0
operator:11

the fields in above example we are printing using ‘:’ delimiter.

6. Print only when delimiter is exists

If you can observe above 5th example we used an delimiter ‘:’ (colon ). Now i will show how the cut command will print without delimiter.

[root@NagiosServer temp]# cat test.txt |cut -f1
Tech tutorials is a website where you can learn lot og things like
computer hardware, computer networking: Linux; SAN, NAS, DAS
so many certifications and againing knowledge freely

its printed all the text in above example, conclusion is we can’t print an fields without using an delimiter using cut command.

7. Print all fields except specified field

The following example will print all the fields except specified. We have to use -complement option

[root@NagiosServer temp]# cat /etc/passwd | cut -d':' --complement -s -f5
root:x:0:0:/root:/bin/bash
bin:x:1:1:/bin:/sbin/nologin
daemon:x:2:2:/sbin:/sbin/nologin
adm:x:3:4:/var/adm:/sbin/nologin
[root@NagiosServer temp]# cat /etc/passwd | cut -d':' --complement -s -f1
x:0:0:root:/root:/bin/bash
x:1:1:bin:/bin:/sbin/nologin
x:2:2:daemon:/sbin:/sbin/nologin
x:3:4:adm:/var/adm:/sbin/nologin

8. Change the delimiter in display

See the below example, changing the display delimiter as per the requirement.

[root@NagiosServer temp]# cat /etc/passwd |grep root   <<-- Actual File output
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@NagiosServer temp]# cat /etc/passwd |grep root |cut -d':' -s -f1,4,7 --output-delimiter='##' <<-- Changed
root##0##/bin/bash
operator##0##/sbin/nologin

9. Change Output Delimiter to New Line

In this example, each and every field of the cut command output is displayed in a separate line. We still used –output-delimiter, but the value is $’\n’ which indicates that we should add a newline as the output delimiter.

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

Conclusion:

cut command is used to print specified number of characters and fields.

Your feedback is valuable to us….

Related Articles

Shell Scripting Class 2 Variables

What is Shell Scripting

What is Shell and Shell Scripting

Thanks for your wonderful Support and Encouragement