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:
Let's first take a look at some options of cut. The cut command must be specified with options to be executed. We take the following text file named $ 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 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 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 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 $ 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 The output is:
How to extract content as characters The 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 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:
|
>>: An article to master MySQL index query optimization skills
Docker installation Use the official installation...
There are many methods on the Internet that, alth...
There is an interview question that requires: a th...
It has to be said that a web designer is a general...
1. Set up Chinese input method 2. Set the double ...
Copy code The code is as follows: <html> &l...
Table of contents 1. Solution 2. MySQL character ...
MySQL Query Cache is on by default. To some exten...
Everyone knows that images on web pages are genera...
When developing a website function, the session c...
Table of contents 1. Vue3 component communication...
I finished reading "Patterns for Sign Up &...
Recently, I have done a simple study on the data ...
In the recent project, we need to create an effec...
Install MySQL database a) Download the MySQL sour...