文本操作(1)——cut

[prophet@localhost ~]$ cut –help
用法:cut [选项]… [文件]…
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.
-b, –bytes=列表 只选中指定的这些字节
-c, –characters=列表 只选中指定的这些字符
-d, –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 补全选中的字节、字符或域
-s, –only-delimited 不打印没有包含分界符的行
–output-delimiter=字符串 使用指定的字符串作为输出分界符,默认采用输入
的分界符
–help 显示此帮助信息并退出
–version 显示版本信息并退出

仅使用f -b, -c 或-f 中的一个。每一个列表都是专门为一个类别作出的,或者您可以用逗号隔
开要同时显示的不同类别。您的输入顺序将作为读取顺序,每个仅能输入一次。
每种参数格式表示范围如下:
N 从第1 个开始数的第N 个字节、字符或域
N- 从第N 个开始到所在行结束的所有字符、字节或域
N-M 从第N 个开始到第M 个之间(包括第M 个)的所有字符、字节或域
-M 从第1 个开始到第M 个之间(包括第M 个)的所有字符、字节或域

当没有文件参数,或者文件不存在时,从标准输入读取

GNU coreutils online help:

[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
[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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值