每天一个Linux命令(9) cut

cut

cut 切
cut命令用来显示行中的指定部分,删除文件中指定字段。cut经常用来显示文件的内容,类似于下的type命令。

说明:该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将它们的内容输出到标准输出上;其二是连接两个或多个文件,如cut fl f2 > f3将把文件fl和几的内容合并起来,然后通过输出重定向符“>”的作用,将它们放入文件f3中。

当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容。因此,一般用more等命令分屏显示。为了控制滚屏,可以按Ctrl+S键,停止滚屏;按Ctrl+Q键可以恢复滚屏。按Ctrl+C(中断)键可以终止该命令的执行,并且返回Shell提示符状态。

语法
cut(选项)(参数)
选项

-b:仅显示行中指定直接范围的内容;
-c:仅显示行中指定范围的字符;
-d:指定字段的分隔符,默认的字段分隔符为“TAB”;
-f:显示指定字段的内容;
-n:与“-b”选项连用,不分割多字节字符;
--complement:补足被选择的字节、字符或字段;
--out-delimiter=<字段分隔符>:指定输出内容是的字段分割符;
--help:显示指令的帮助信息;
--version:显示指令的版本信息。

参数
文件:指定要进行内容过滤的文件。

实例
例如有一个学生报表信息,包含No、Name、Mark、Percent:

[root@localhost text]# cat test.txt
No Name Mark Percent
01 tom 69 91
02 jack 71 87
03 alex 68 98
使用 -f 选项提取指定字段:

[root@localhost text]# cut -f 1 test.txt
No
01
02
03
[root@localhost text]# cut -f2,3 test.txt
Name Mark
tom 69
jack 71
alex 68
--complement 选项提取指定字段之外的列(打印除了第二列之外的列):

[root@localhost text]# cut -f2 --complement test.txt
No Mark Percent
01 69 91
02 71 87
03 68 98
使用 -d 选项指定字段分隔符:

[root@localhost text]# cat test2.txt
No;Name;Mark;Percent
01;tom;69;91
02;jack;71;87
03;alex;68;98
[root@localhost text]# cut -f2 -d";" test2.txt
Name
tom
jack
alex
指定字段的字符或者字节范围
cut命令可以将一串字符作为列来显示,字符字段的记法:

N-:从第N个字节、字符、字段到结尾;
N-M:从第N个字节、字符、字段到第M个(包括M在内)字节、字符、字段;
-M:从第1个字节、字符、字段到第M个(包括M在内)字节、字符、字段。
上面是记法,结合下面选项将摸个范围的字节、字符指定为字段:

-b 表示字节;
-c 表示字符;
-f 表示定义字段。
示例

[root@localhost text]# cat test.txt
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
打印第1个到第3个字符:

[root@localhost text]# cut -c1-3 test.txt
abc
abc
abc
abc
abc
打印前2个字符:

[root@localhost text]# cut -c-2 test.txt
ab
ab
ab
ab
ab
打印从第5个字符开始到结尾:

[root@localhost text]# cut -c5- test.txt
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz

CUT(1)
Commands

NAME

   cut - remove sections from each line of files

SYNOPSIS

   cut OPTION... [FILE]...

DESCRIPTION

   Print selected parts of lines from each FILE to standard output.

   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     with -b: don't split multibyte characters

   --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

   --help display this help and exit

   --version
          output version information and exit

   Use  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 field

   With no FILE, or when FILE is -, read standard input.

   GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report cut translation bugs to <http://translationproject.org/team/>

AUTHOR

   Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

COPYRIGHT

   Copyright © 2013 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
   This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

   The full documentation for cut is maintained as a Texinfo manual.  If the info and cut programs are properly installed at your site, the command

          info coreutils 'cut invocation'

   should give you access to the complete manual.

本文链接:

https://heyzen.club/index.php/Coder/122.html
1 + 8 =
快来做第一个评论的人吧~