[Copyright statement: reprinted. Please retain the Source: blog.csdn.net/gentleliu. Mail: shallnew at 163 dot com]
The cut command is similar to awk. It extracts information from the line and is an awk of the feature weakening version.
The format of the cut command is: Cut [Options] filename.
Options:
-D specifies the domain separator different from space and t a B key. Similar to awk's "-F ".
-F field specifies the number of cut Fields
-C List specifies the number of characters to cut.
First, we will process the split password file:
# cat passwdroot:x:0:0:root:/root:/bin/shproxy:x:13:13:proxy:/bin:/bin/shoperator:x:37:37:Operator:/var:/bin/shftp:x:83:83:ftp:/home/ftp:/bin/shnobody:x:99:99:nobody:/home:/bin/shsshd:x:103:99:Operator:/var:/bin/sh
Split by:, and then take the first and sixth columns.-F can specify the number of obtained fields:
# cut -d: -f1,6 passwdroot:/rootproxy:/binoperator:/varftp:/home/ftpnobody:/homesshd:/var
The specified number of domains can also be written as follows:
# cut -d: -f 1-4,6 passwdroot:x:0:0:/rootproxy:x:13:13:/binoperator:x:37:37:/varftp:x:83:83:/home/ftpnobody:x:99:99:/homesshd:x:103:99:/var
Awk implementation:
# awk -F: 'BEGIN{OFS=":"}{print $1,$2,$3,$4,$6}' passwd root:x:0:0:/rootproxy:x:13:13:/binoperator:x:37:37:/varftp:x:83:83:/home/ftpnobody:x:99:99:/homesshd:x:103:99:/var
However, if you want to specify multiple characters for segmentation, cut will not work. Cut only supports a single separator, and two awk supports multiple. For example, we separate the values with "bin:
# awk -F"bin" 'BEGIN{OFS=":"}{print $1}' passwdroot:x:0:0:root:/root:/proxy:x:13:13:proxy:/operator:x:37:37:Operator:/var:/ftp:x:83:83:ftp:/home/ftp:/nobody:x:99:99:nobody:/home:/sshd:x:103:99:Operator:/var:/
Use cut:
# cut -d"bin" -f1 passwd cut: the delimiter must be a single characterTry 'cut --help' for more information.
We can see that the cut command has limited functions.
By default, cut is separated by the tab key. awk is separated by spaces or multiple spaces or tabs by default:
# sed 's/:/\t/g' passwd | cut -f5rootproxyOperatorftpnobodyOperator# sed 's/:/\t/g' passwd | awk '{print $5}'rootproxyOperatorftpnobodyOperator
Cut can cut any character. It seems to be powerful, but it is rarely useful. Use the-C option to specify the exact cut quantity. This method requires you to know the start and end characters. This method is usually not used unless it is in a fixed-length domain or file name.
Let's look at several examples:
# cut -c1,2,4 passwdrotprxoprft:noossd
# cut -c5-9 passwd :x:0:y:x:1ator:x:83:dy:x::x:10
Shell text filtering programming (10): Cut command