Linux Cut Command with Practical Example

Fault! The file name is not specified.

The cut command is used in Linux and Unix systems to cut parts and partitions from each line of the file and write the result to the default output. Can be used to cut off parts of a line byte position, character and field (separator).

In this tutorial we will study the Linux Cut command using some practical examples that you can use in your daily work with the command line.

Cutting command and syntax

The main syntax of the Cut command is as follows:

WARNING… PHILB…

Let’s look at the reduction options, and without this option the reduction command will not work.

Reduction options

-f : Extract by specifying the field. The Cut command uses TAB as the default field separator.

-d : The tab is the default limiter, and with this option you can use a specific limiter.

-b : To retrieve, give up a byte. You can also specify a byte range.

-c : Carved by character. It can be a list of numbers separated by a comma, or a sequence of numbers separated by a hyphen (-).

-Completion: This completes the selection

-output distributor : Use the output delimiter=separator option to change the output delimiter.

-Just demarcated. -Just demarcated: Cutting does not print strings that do not have separators.

In this tutorial we use the following text file called content.txt and the /etc/passwd file to illustrate our examples.

cat content.txt
Ubuntu Linux
Microsoft Windows
OsX El Capitan
Unix
FreeBSD

How to cut with the separator

The most commonly used cutting option is the -d and -f combination. It will mainly extract the content according to certain listed separators and fields.

For example, if you use the separator (:), only the first field in each line of the /etc/passwd file will be printed.

$ cut -d’:’ -f1 /etc/passwd
root
bin
daemon
adm
lp
synchronization
shutdown
shutdown
mail
operator
game

In the following example, we use a space ( ) as a separator and cut the first field of a file named content.txt.

cut -d -f 1 content.txt
Ubuntu
Microsoft
OsX
Unix
FreeBSD

This example extracts more than one field from a specific file. Here we get the first and the sixth out. Field using the colon (:) separator of the file ‘/etc/passwd’ containing the string ‘/bin/bash’:

grep /bin/bash /etc/passwd | cut -d’:’ -f1.6
root:/root
lettuce:/home/slax

To display the range of the fields, specify a start and end field separated by a (-) character, as shown below:

$ grep /bin/bash /etc/passwd | cut -d’:’ -f1-4,6,7
root:x:0:0:/root:/bin/bash
slax:x:1000:/home/slax:/bin/bash

How to complete the product selection

Use the -complete option to complete the list of selection fields. This option is used to select all fields except the specified ones.

In the following example, the command prints all fields except the 2nd field. in the /etc/passwd file:

$ grep /bin/bash /etc/passwd | cut -d’:’ –complement -f2
root:0:0:root:/root:/bin/bash

How to specify the output splitter

Use the –output delimiter option to specify the output divider. The input separator is set with option -d, and by default the output separator is the same as the input separator.

Let’s start checking the output without using an output splitter, as follows:

Cut in $-d: -f1.7 /etc/passwd | sort | uniq -u_apt:/usr/sbin/nologinbackup:/usr/sbin/nologindaemon:/usr/sbin/nologindaemon/usr/sbin/nologindnsmasq :/usr/sbin/nologinGames:/gnats:/usr/sbin/nologinirc:/usr/sbin/nologinLandscape:/usr/sbin/nologinList:/usr/sbin/nologinlp:/usr/sbin/nologinlxd:/bin/false

I have now added the option –output delimiter, and the two points of the input limiter (:) have been replaced by the SPACE output limiter, as follows

Cut: -f1,7 –Output Limiter ‘ ‘ /etc/passwd | sort | single -u_apt /usr/sbin/nologinSecurity /usr/sbin/nologinbin /usr/sbin/nologinDemon /usr/sbin/nologinnsmasq /usr/sbin/nologinGame /usr/sbin/nologinMosquitoes /usr/sbin/nologinirc /usr/sbin/nologinList /usr/sbin/nologinlp /usr/sbin/nologinlxd /bin/false

Let’s take another example, here we use an output separator to execute on each field of a new line.

Here we’re using –output delineation as $’n’, which indicates a new rule.

Check the exit:

grep root /etc/passwd | cut -d’:’ -f1,6,7 –output-delimiter=$’n’
root
/root
/bin/bash
operator
/root
/sbin/nologin

How to cut through symbols

The -c (column) option is used to cut the symbol positions. Don’t forget that TABS and spaces are also treated as symbols.

To print the first character of each line of the file named content.txt, use :

$ cut -c 1 content.txt
U
M
O
U
F

In the following example we show the characters from 1 to 7 (range) of each line of the :

cut -c 1-7 content.txt
Ubuntu
Microso
OsX El
Unix
FreeBSD

Let’s see how columns can be selected by a specific start or end position.

To take the columns out of the 2nd until the last sign:

cut -c2- content.txt
buntu Linux
icrosoft Windows
sX El Capitan
nix
reeBSD

To extract columns of 1 to 4 characters:

cut -c-4 content.txt
Ubun
Micr
OsX
Unix
free

How to break down into bytes

Use the -b option to select part of the string by specifying a byte position separated by numbers separated by a comma (,). The hyphen allows you to specify a range of bytes.

The following example crosses the 1st, 2nd and 3rd of a file called content.txt :

cut -b 1,2,3 content.txt
Ubu
Mic
OsX
Uni
Fre

We can also create a list of zones with the following command:

cut -b 1-3.5-7 content.txt
Ubutu
Micoso
OsXEl
Uni
FreBSD

Some practical examples of reduction

Cutting is more convenient when combining different Linux or Unix commands.

For example, you want to unpack ‘USER’, ‘PID’ and COMMAND with the command ps:

ps -L u n | tr -s | cut -d -f 2,3,14 –
USER PID COMMAND
0 676 /sbin/agetty -o -p — u –keep-baud 115200,38400,9600 ttyS0 vt220
0 681 /sbin/agetty -o -p — u –noclear tty1 linux
0 23174 -bash
0 26737 ps -L u n
0 26738 tr -s
0 26739 cut -d -f 2.3.14 —

Take another example to extract the values from the total, used and free memory and store them in a text file with different commands :

free -m |tr -s ”. | sed ‘/^Mem/!’ | cut -d -f2-4 >> memory.txt
Output
$ cat memory.txt
985 86 234

Conclusion

The cut command can be combined with many other Linux or Unix commands. It can be equipped with one or more filters for additional word processing.

One of the limitations of the Cut command is that it is not possible to specify more than one character as a separator. Multiple spaces are considered multiple field separators, and the tr command must be used for cutting to get the desired result.linux cut delimiter tab,cut command in windows

You May Also Like