Option Description
cut command line dividing the specified delimiters into columns, its weakness is hard to deal with the case where a plurality of separators repeated, thus often combined tr compression feature.
-b
: Byte screening; -n
: the "-b" option conjunction, that would prohibit bytes separated operate; -c
: Filter by character; -f
: by field screening; -d
: Specifies the field separator, the default field when not write separate -d Fu is "TAB"; and therefore only "-f" option is used with. -s
: Print lines that do not prevent delimiters; --complement
: complement selected byte, character or field (or reverse selection means is set up); : --output-delimiter
Specifies the output delimiter; default delimiter input.
Assumed that the content shown in the following /tmp/abc.sh. Note: line 2 to line 5 of each column are not separated by a single space, and in some places repeated several spaces, in some places only one space, that is, the text is not very regular. And the last line is no box.
The following are examples of cut.
There are five fields in the abc.sh. The second screening field name column and the fourth column mark field. Spaces as a separator.
[root@localhost ~]# cut -d" " -f2,4 abc.sh Name Mark 001 001 djakldj;lajd;sla
You can see, the output is a mess of unintended consequences. The reason is that the delimiter separated by a space where repeated many times. So you want to display the results correctly, you need to repeat a space to dispose of.
You can use tr tool to compress consecutive characters.
[root@localhost ~]# cat abc.sh | tr -s " " | cut -d " " -f2,4 Name Mark longshuai 56 gaoxiaofang 60 zhangsan 50 lisi 80 wangwu 90 djakldj;lajd;sla
But output in the last row delimiter line is also no output, which requires the use -s to cancel such an output.
[root@localhost ~]# cat abc.sh | tr -s " " | cut -d" " -f2,4 -s Name Mark longshuai 56 gaoxiaofang 60 zhangsan 50 lisi 80 wangwu 90
Use --complement
In addition to the output of the second field and the fourth field in the rest of all the fields.
[the root @ localhost ~] # CAT . ABC SH | TR -s " " | Cut -d " " -f2, . 4 -s - complement NO subjectid Notes . 1 001 Fail 2 001 pass . 3 001 fail . 4 001 pass 5 001 pass
Byte or character segmentation
English and Arabic numerals are single-byte characters, Chinese double-byte characters, and even 3-byte characters.
-B bytes to use screened by using the -c character segmentation.
Note that when the division is not specified -d bytes or characters, as is divided -d field.
[root@localhost ~]# cut -b1-5 abc.sh NO Na 1 lo 2 ga 3 zh 4 li 5 wa djakl
As the Chinese screening, the results appear garbled.
[root@localhost ~]# cut -b20 abc.sh a 6 0
So "-b" option requires a combination of "-n" option to disable the "-b" option will force the split multi-byte characters cause garbled.
[root@localhost ~]# cut -n -b20 abc.sh a 6 不 0
It can also be separated by character.
[root@localhost ~]# cut -c20 abc.sh a 6 不 0
Use --output-delimiter
Use "--output-delimiter" delimiter specified output.
When using the -b or -c separates the multi-stage character, you can use --output-delimiter, otherwise the multistage spliced together.
[root@localhost ~]# cut -b3-5,6-8 abc.sh Name longs gaoxi zhang lisi wangw akldj;
[root@localhost ~]# cut -b3-5,6-8 abc.sh --output-delimiter "," Na,me lo,ngs ga,oxi zh,ang li,si wa,ngw akl,dj;
cut in the specified range
Can use "N -", "NM" and "-M" represent the content of each row of all N characters (or bytes or fields) after the contents of the previous paragraphs NM and M segments. Note that N and M, inclusive.
[the root @ localhost ~] # Cut -d " " -f3- ABC. SH - S subjectid Mark Remarks longshuai 001 56 is failing gaoxiaofang 001 60 pass zhangsan 001 50 failed Lisi 001 80 pass wangwu 001 90 pass
When the cross-range, the output will not be repeated. For example -f3-5,4-6, the output -f3-6.
[root@localhost ~]# cut -d" " -f3-5,4-6 abc.sh -s SubjectID Mark 备注 longshuai 001 56 gaoxiaofang 001 60 zhangsan 001 50 不及格 lisi wangwu 001
If the range of random sequence, then the output will first Linux sort of range (in ascending order). E.g. -f4-6,2 equivalent to -f2,4-6.
[the root @ localhost ~] # Cut -d " " -f4- . 6 , 2 ABC. SH - S the Name Remarks Mark 001 56 is 001 60 001 50 failed 001