cut command linux

Cut Command in Linux


Working in GNU/Linux terminal often includes the processing of outgoing text. One of the most common tools that is used for this is the cut command. It cuts out some text information (file or standard output) by the specified filter.

This procedure is often necessary to analyze complex data structures and, if necessary, further transfer the text to other tools. Cut is built-in by default on all GNU/Linux distributions. This article describes its description and examples of use.

Syntax and Options of Cut Command in Linux

The general view of cut is as follows:

cut OPTION [FILE]

The FILE parameter in square brackets indicates that it is optional. Thus, this explains that cut can work with data from other utilities through pipe.

Work with the file occurs line by line. The result of its work is output to the standard output stream of processed information. No changes are made to the files themselves.

The following is a list of cut options:

Short (BSD) style Long (GNU) style Value
-b LIST –bytes=LIST cut characters byte by their LIST numbers, where LIST can take, for example, such forms:

  • 1 (one byte)
  • 1, 5, 12 (specific bytes)
  • 5-15 (byte range)
-c LIST –characters=LIST similarly -b; the difference is that -c reads one character regardless of its encoding
-d DELIM –delimiter=DELIM set your own delimiter (Tab is used by default)
-f LIST –fields=LIST specify field numbers for cutting
-s –only-delimited do not output lines where there is no separator
–complement specify bytes, characters or fields to cut; everything else will be saved
–output-delimiter=STRING by default, the output delimiter is equal to the input; this option sets another output delimiter
-z –zero-terminated use NULL as a newline symbol

Examples of Using the Cut Command

Let’s take a look at some examples of the cut utility.

Working with Characters

It’s important to mention that working with English letters is somewhat easier. This is due to the fact that each English character occupies one byte. The letters of other languages, as well as many special characters that (including) are not on the keyboard, occupy 2 or more bytes. You should remember this when using the -b and -c options, but for the letters of the English alphabet there is no difference between them.

For example, use a file with the following contents:

linux cut command

Cut the first 4 characters (there may be an empty space or a unit denoting the first character before the dash sign):

cut -c -4 example.txt

linux cut

Cut 3 to 10 characters:

cut -c 3-10 example.txt

linux cut examples

Please note that the tab character is considered one character, so in the second line even the third field is captured. If instead of tabs there were spaces, the utility would consider each space as a separate character.

When working with cut bytes, fields and their delimiters are not involved, it simply cuts the text according to the specified parameters.

bash cut command

Working with Fields

For a more human-friendly use of cut command, you should work not with bytes, but with fields. As already mentioned, by default, the program considers the Tab character to be a delimiter.

Let’s go back to our file and display the first and third fields:

cut -f 1,3 example.txt

linux cut last field

If you try to use such a record, for example, for the first five lines of the /etc/passwd file, the result will be the following:

head -5 /etc/passwd | cut -f 1,3

linux cut command examples

This text doesn’t differ from original text, because here the creator of this file conceived a colon sign as a field separator. Therefore, here you need to add the -d option indicating this character, after which the program will produce the expected result:

head -5 /etc/passwd | cut -f 1,3 -d :

linux cut column

Note. In order to use a space as a delimiter, enclose it in quotation marks to get -d “ “.

Data Exclusion

The –complement option is the inverse of the standard way of outputting information, excluding text according to the specified template.

In the following example, the second and third fields are excluded from the output, as well as from the fifth to the last field:

head -5 /etc/passwd | cut --complement -f 2-3,5- -d :

linux cut file

Useful System Scripts

A special benefit of the cut utility is working with the system data. Each of the following set of commands can be an independent script of the shell or be part of a more complex one.

Display the amount of free RAM in a human-readable form:

free -h | grep Mem | sed 's/\s\+/,/g' | cut -d , -f 7

bash cut line

Show the MAC address of all available network cards of the current computer:

ip a | grep ether | sed ‘s/\s\+/,/g’ | cut -f3 -d,

linux bash cut

Show all services using the specified port (in this case, 80):

grep -w 80 /etc/services | cut -f 1 | uniq

linux shell cut

Displays the full name of the CPU:

cat /proc/cpuinfo | grep "name" | cut -d : -f2 | uniq

using cut in bash

Add Your Comment