cut命令可以从文本中剪切 列 或 域
可选项有:
-c,指定剪切字符数。
-c 1,剪切第一个字符;
-c 5,剪切第五个字符;
-c 1-5剪切第一到第五个字符;
-c 1,3-5,剪切第一个字符,剪切第三到五个字符。
-f,指定剪切域数。(需要先分割出域来,否则输出所有的内容)
-f 1,5,剪切第一和第五域;
-f 1-3,剪切第一到第三域;
-f 1,3-5,剪切第一域和第三到第五域。
-d,域分隔符,
-d:,以冒号分割,如果需要观察第二域,加上-f2
-d: -f2
ubuntu@VM-0-7-ubuntu:~/shellScript$ who
ubuntu pts/0 1234-56-78 90:12 (256.257.258.259)
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f1
ubuntu
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f2
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f3
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f4
pts/0
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f5
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f6
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f7
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f8
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f9
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f10
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f11
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f12
1234-56-78
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f13
90:12
ubuntu@VM-0-7-ubuntu:~/shellScript$ who | cut -d ' ' -f14
(256.257.258.259)
cut的官方说明:
Usage: cut OPTION… [FILE]… Print selected parts of lines from each
FILE to standard output.With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options
too. -b, --bytes=LIST select only these bytes -c,
–characters=LIST select only these characters -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter -f, --fields=LIST select only these fields; also print any line
that contains no delimiter character, unless
the -s option is specified -n (ignored)
–complement complement the set of selected bytes, characters
or fields -s, --only-delimited do not print lines not containing delimiters
–output-delimiter=STRING use STRING as the output delimiter
the default is to use the input delimiter -z, --zero-terminated line delimiter is NUL, not newline
–help display this help and exit
–version output version information and exitUse one, and only one of -b, -c or -f. Each LIST is made up of one
range, or many ranges separated by commas. Selected input is written
in the same order that it is read, and is written exactly once. Each
range is one of: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 fieldGNU coreutils online help: https://www.gnu.org/software/coreutils/
Full documentation at: https://www.gnu.org/software/coreutils/cut or
available locally via: info ‘(coreutils) cut invocation’