Linux cut command explained

Linux cut command explained

The cut command in Linux and Unix is ​​used to cut out some parts from each line of a file and output them to the standard output. We can use the cut command to extract a portion of the content from a line of string in units of bytes, characters, fields (separators), etc.

In this article, we will learn about the use of cut command through some examples. These usage methods are also very commonly used in our daily work.

Cut Command and Syntax

The basic syntax of the cut command is as follows:

$ cut OPTION... [FILE]...

Let's first take a look at some options of cut. The cut command must be specified with options to be executed.

-f : Extract the specified field. The cut command uses Tab as the default separator.

-d : Tab is the default delimiter, use this option to specify your own delimiter.

-b : Extract the specified bytes, or specify a range.

-c : Extract the specified characters, which can be a list of numbers separated by commas or a range of numbers separated by hyphens.

–complement : complement the selected part, i.e. invert the selection.

–output-delimiter : Change the delimiter used when outputting.

--only-delimited : Do not output columns that do not contain delimiters.

We take the following text file named context.txt and /etc/passwd file as examples to illustrate.

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

How to specify the delimiter

The most commonly used options are the combination of -d and -f , which extracts content based on the delimiter specified by -d and the fields listed by -f .

For example, in this example, only the first field of each line of the /etc/passwd file is printed, and the separator used is :

$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...

In the following example we print the first field of the content.txt file using spaces as delimiters.

$ cut -d " " -f 1 content.txt 
Ubuntu
Microsoft
OxY
Unix
FreeBSD

In the example below we extract multiple fields. Here, we extract the first and sixth fields from the line containing the string /bin/bash in the file /etc/passwd using the colon (:) delimiter.

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
alvin:/home/alvin

To display a range of fields, you can specify the starting and ending fields, separated by a hyphen (-), as shown below:

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

How to complete the selected output

To complete the selected output fields (i.e., deselect them), use the --complement option. This option outputs all fields except the specified fields.

The following example prints all fields except the second field in the line containing /bin/bash 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 delimiter

Use --output-delimiter to specify the output delimiter. The input delimiter is specified by -d , and the output delimiter is the same as the input delimiter by default.

Let's first use the following example to test the output when the output delimiter is not specified;

$ cut -d: -f1,7 /etc/passwd | sort | uniq -u
_apt:/usr/sbin/nologin
backup:/usr/sbin/nologin
bin:/usr/sbin/nologin
daemon:/usr/sbin/nologin
dnsmasq:/usr/sbin/nologin
games:/usr/sbin/nologin
gnats:/usr/sbin/nologin
irc:/usr/sbin/nologin
landscape:/usr/sbin/nologin
list:/usr/sbin/nologin
lp:/usr/sbin/nologin
lxd:/bin/false

Now we add the --output-delimiter option to specify the output delimiter as a space:

$ cut -d: -f1,7 --output-delimiter ' ' /etc/passwd | sort | uniq -u
_apt /usr/sbin/nologin
backup /usr/sbin/nologin
bin /usr/sbin/nologin
daemon /usr/sbin/nologin
dnsmasq /usr/sbin/nologin
games /usr/sbin/nologin
gnats /usr/sbin/nologin
irc /usr/sbin/nologin
landscape /usr/sbin/nologin
list /usr/sbin/nologin
lp /usr/sbin/nologin
lxd /bin/false

Let's test another example and use a separator to print one field per line.

We specify --output-delimiter as $'\n' to indicate a newline.

The output is:

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

How to extract content as characters

The -c option can be used to extract based on character position. Note that spaces and Tab are also treated as characters.

Print the first character of each line of the context.txt file, as follows:

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

The following shows the first seven characters of each line of the context.txt file;

$ cut -c 1-7 content.txt
Ubuntu
Microso
XOt
Unix
FreeBSD

Let's test again by specifying only the start or end position.

The following extracts the second to last characters:

$ cut -c2- content.txt
Ubuntu Linux
Microsoft Windows
sX El Capitan
nix
reeBSD

Extract the first to fourth characters:

cut -c-4 content.txt
Ubun
Micr
OxY
Unix
Free

How to extract by bytes

Use the -b option to select part of a line by specifying byte positions, using commas to separate each specified position, or - to specify a range.

The following example extracts the first, second, and third bytes of each line in the content.txt file:

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

We can also list a range using the following command;

$ cut -b 1-3,5-7 content.txt 
Ubutu 
Micoso
XOtM 
Uni
FreBSD

Some practical examples

cut is a useful command that is often used in conjunction with other Linux or Unix commands.

For example, if you want to extract USER, PID and COMMAND from the ps command:

ps -L un | 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 un
0 26738 tr -s
0 26739 cut -d -f 2,3,14-

Let's test another example to extract the total, used, and free values ​​of the memory and save them to a file.

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

Summarize

The cut command can be connected to many other Linux or Unix commands through pipes. One or more filters can be piped through for additional text processing.

One of the limitations of the cut command is that it does not support specifying multiple characters as delimiters. Multiple spaces are counted as multiple field separators, so the tr command must be used before the cut command to obtain the desired output.

This is the end of this article about the detailed explanation of Linux cut command. For more relevant Linux cut command content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use of Linux gzip command
  • Usage of Linux userdel command
  • Use of Linux date command
  • How to run Linux commands in the background
  • Use of Linux stat command
  • Use of Linux ls command
  • Use of Linux ln command
  • Use of Linux bzip2 command

<<:  Detailed explanation of the organizational structure diagram case of Vue's vue-tree-color component

>>:  An article to master MySQL index query optimization skills

Recommend

Detailed steps for porting busybox to build a minimal root file system

Busybox: A Swiss Army knife filled with small com...

Three steps to solve the IE address bar ICON display problem

<br />This web page production skills tutori...

Research on the Input Button Function of Type File

<br />When uploading on some websites, a [Se...

JavaScript Canvas implements Tic-Tac-Toe game

This article shares the specific code of JavaScri...

Solution to the horizontal scroll bar in iframe under IE6

The situation is as follows: (PS: The red box repr...

Tutorial on installing MySQL 5.7.9 using RPM package under CentOS 7

Recorded MySQL 5.7.9 installation tutorial, share...

Detailed explanation of docker network bidirectional connection

View Docker Network docker network ls [root@maste...

Complete steps to use vue-router in vue3

Preface Managing routing is an essential feature ...

Tutorial on deploying the open source project Tcloud with Docker on CentOS8

1. Install Docker 1. I installed Centos7 in the v...

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

CSS3 custom scroll bar style::webkit-scrollbar sample code detailed explanation

The default scroll bar style in Windows is ugly, ...

Some useful meta setting methods (must read)

<meta name="viewport" content="...

CentOS6.8 Chinese/English environment switching tutorial diagram

1. Introduction People who are not used to Englis...