How to use linux cut command

29/12/2020
Chưa phân loại
The Linux cut command is useful to display file’s lines content, dividing the content as we want. The command cut can cut lines by characters, based on a delimiter or by bytes.

If we get a list of e-mails or items separated by commas or spaces we can use the command cut to read the content easily.

To start let’s create a file

$ nano filecut

With content:

mail1@domain1.com, mail2@domain2.com, mail3@domain3.com, mail4@domain4.com

Press ctrl+x to save.

The fake e-mail addresses are separated by comma and space, to list the content between comma you need to use the parameter -d (delimiter), type:

$ cut -d ‘,’ -f1 <FileName>
mail1@domain1.com

We can also show more fields you can also use the parameter ‘f, type:

$ cut -d ‘,’ -f 1,2,3 filecut
mail1@domain1.com, mail2@domain2.com, mail3@domain3.com

$ nano abc
With content;
abcdefghijklmn

Now let’s use the command to show the first 4 characters by bytes, 1 char = 1 byte:

:
# cut -b 1,2,3,4 abc
abcd

The command cut allows us to display ranges of bytes, to display between the byte 2 and 7 type:

# cut -b 2-7 abc
bcdefg

Now let’s create a document with words:

$ nano words
And include words:
word1
word2
word3
word4
word6
word7

Press ctrl+x to save and close, to list

Let’s use the command cut to list the fifth character of each line:

$ cut -b 5 words
1
2
3
4
5
6
7

Create a file called columns:

nano columns
With content (separated with TAB,not space):
column1line1   column2line1   column3line1   column4line1
column1line2   column2line2   column3line2   column4line2
column1line3   column2line3   column3line3   column4line3
column1line4   colmun2line4   column3line4   column4line4

Now use cut to show the third field by typing:

$ cut -f 3 columns
column3line1
column3line2
column3line3
column3line4

As you see cut displays the content of the third column, we can display more than one column with a single order:

$ cut -f 1,3 columns
column1line1    column3line1
column1line2    column3line2
column1line3    column3line3
column1line4    column3line4

Let’s take a look at the /etc/passwd file and display it’s segments with cut.

$ less /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
:

Segments are separated by ":", lets display the first field, before the ":":
# cut -f 1 -d ‘:’ /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-timesync
systemd-network
systemd-resolve
systemd-bus-proxy

We can display more than one column by adding more fields after the parameter ‘f:

# cut -f 1,6 -d ‘:’ /etc/passwd
root:/root
daemon:/usr/sbin
bin:/bin
sys:/dev
sync:/bin
games:/usr/games
man:/var/cache/man
lp:/var/spool/lpd
mail:/var/mail
news:/var/spool/news
uucp:/var/spool/uucp
proxy:/bin
www-data:/var/www
backup:/var/backups
list:/var/list
irc:/var/run/ircd
gnats:/var/lib/gnats
nobody:/nonexistent
systemd-timesync:/run/systemd

The command above lists first and sixth columns separated by “:”, we see the user username and home directory.

I hope this tutorial was useful as an introduction to the command cut, for more information on this command type “man cut”, should you have any inquiry contact us opening a ticket support at LinuxHint Support. Keep following LinuxHint for more tips and updates on Linux.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Hướng dẫn cấu hình nginx với ssl làm reverse proxy cho graylog

Gray log là 1 công cụ mạnh mẽ để quản lý và phân tích các tệp nhật ký, ở các bài trước mình...
30/12/2020

Hướng dẫn sử dụng Proxy Helper Fakeip khi thuê proxy

Theo mặc định, Chrome sử dụng cài đặt proxy hệ thống. Nhưng đôi khi bạn muốn chỉ đặt proxy...
21/11/2022

Install PIP on Ubuntu

Install PIP on Ubuntu 17.10 Artful Aardvark Python is a widely used general purpose programming language. Python is very...
12/02/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025