cut command in linux with example

In this tutorial, I am use cut command to remove sections from each line of files.

What does cut command mean?

cut – remove sections from each line of files

Syntax

cut OPTION... [FILE]...

In the man page, the describe it

  • cut – remove sections from each line of files.
  • man cut – More details information about cut command.

cut command in Linux with 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 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

Conclusion

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

,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. 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 *

Comment moderation is enabled. Your comment may take some time to appear.

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