cut command in Linux with example

Introduction

In this tutorial, I am using the cut command in Linux to remove sections from each line of files.

The cut the command is used to extract specific sections (columns) from lines of input text or files in Linux and Unix systems. It is particularly useful for working with delimited data.

What does cut command mean?

cut – remove sections from each line of files

Syntax

cut OPTION... [FILE]...

On the man page, the description it

  • cut – remove sections from each line of files.
  • man cut – More details information about the cut command.
cut command in linux with example

cut command in Linux with an example

I have created a file cut_command.txt as below

[vagrant@DevopsRoles ~]$ cat cut_command.txt 
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 Devops Roles.
 Hello world. xxx.

For delimiter-separated fields. The default delimiter is the tab character.

[vagrant@DevopsRoles ~]$ cut -d "," -f 1 cut_command.txt 
 HuuPV
 Devops Roles.
 Hello world. xxx

Get a list of all users in the Linux

[vagrant@DevopsRoles ~]$ cat /etc/passwd | cut -d ':' -f1                                                                                                                                                                              
 root
 bin
 daemon
 adm
 lp
 sync
 shutdown
 halt
 mail
 uucp
 operator
 games
 gopher
 ftp
 nobody
 vcsa
 rpc
 rpcuser
 nfsnobody
 sshd
 exim
 centos
 huupv
 gluster
 grafana
 influxdb
 mysql
 acc1
 netdata
 telegraf
 nginx
 dbus
 haldaemon

cut and sort sort

[vagrant@DevopsRoles ~]$ cat /etc/passwd | grep home | cut -d: -f1,6 | sort
 acc1:/home/acc1
 huupv:/home/huupv

You can also display the table with column -t

[vagrant@DevopsRoles ~]$ cat /etc/passwd | grep home | cut -d: -f1,6 | sort | tr ":" " "  | column -t
 acc1      /home/acc1                                                           
 huupv    /home/huupv

Extract a range of fields

cut -f 2-4 file.txt

Print a specific delimiter

cut -d ',' -f 2 --output-delimiter=" | " file.csv

Read input from a pipe

echo "data1,data2,data3" | cut -d ',' -f 2

Conclusion

cut command in Linux is a simple command in Linux. It is used to remove sections from each line of files.

These are just a few examples of how you can use the cut command. It offers various options and functionalities for extracting specific sections from text or files. You can refer to the cut man page (man cut) for more details and additional options.

Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.