Cut命令详解
cut - remove sections from each line of files
取出文件每一行的指定内容
语法
SYNOPSIS cut OPTION… [FILE]…
描述
Print selected parts of lines from each FILE to standard output.
打印这些内容
options
List
N N'th byte, character or field, counted from 1
N- from N'th byte, character or field, to end of line
N-M from N'th to M'th (included) byte, character or field
-M from first to M'th (included) byte, character or field
List是一个范围如2-3,如N表示取出N位置的内容打印。N-指从N到最后。N-M指从N到M。-M是从开始到M。
打印 tast.txt
name age
David 18
Jack 20
Lisa 32
- -b, --byte=list 选择list指定的内容
[root@myhost ~]# cut -b 1 tesk.txt
n
D
J
L
- -c, --characters=LIST 选择list指定的内容
[root@myhost ~]# cut -c 2 tesk.txt
a
a
a
i
- -d, --delimiter=DELIM 指定分隔符,默认为“TAB“
[root@myhost ~]# cut -d " " -f 1 -n tesk.txt
name
David
Jack
Lisa
- -f, --fields=LIST 字段列表
[root@myhost ~]# cut -d " " -f 2 tesk.txt
age
18
20
32
- -n with -b: don’t split multibyte characters
- –complement 获取list范围以外的内容
[root@myhost ~]# cut -d " " -f 2 --complement tesk.txt
name
David
Jack
Lisa
- -s, --only-delimited
do not print lines not containing delimiters
/*
没有被分割的行,就不显示,默认整行显示
*/
[root@myhost ~]# cut -d "," -f 2 -n tesk.txt
name age
David 18
Jack 20
Lisa 32
[root@myhost ~]# cut -d "," -f 2 -s tesk.txt
[root@myhost ~]#