Polecenia Linuksa

How to use linux cut command

How to use linux cut command
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:

[email protected], [email protected], [email protected], [email protected]

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
[email protected]

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

$ cut -d ',' -f 1,2,3 filecut
[email protected], [email protected], [email protected]

$ 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.

Gry Jak korzystać z GameConqueror Cheat Engine w systemie Linux
Jak korzystać z GameConqueror Cheat Engine w systemie Linux
Artykuł zawiera przewodnik dotyczący korzystania z silnika oszukującego GameConqueror w systemie Linux. Wielu użytkowników, którzy grają w gry w syste...
Gry Najlepsze emulatory konsoli do gier dla systemu Linux
Najlepsze emulatory konsoli do gier dla systemu Linux
W tym artykule wymienimy popularne oprogramowanie do emulacji konsoli do gier dostępne dla systemu Linux. Emulacja to warstwa kompatybilności oprogram...
Gry Najlepsze dystrybucje Linuksa do gier w 2021 r
Najlepsze dystrybucje Linuksa do gier w 2021 r
System operacyjny Linux przeszedł długą drogę od pierwotnego, prostego, serwerowego wyglądu. Ten system operacyjny znacznie się poprawił w ostatnich l...