红联Linux门户
Linux帮助

Linux cut命令学习笔记

发布时间:2014-10-19 16:02:03来源:linux网站作者:chenlei0630

cut命令也是Linux下最常用的命令之一。

正如其名,cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut是以每一行为一个处理对象的,这种机制和sed是一样的。


cut命令的剪切度量有三种:

按字节剪切:-b(byte)
按字符剪切:-c(character)
按域剪切:-f(field)  按域剪切时需要指定分割符,默认分割符是制表符(\t).


此外,cut命令还支持数字表示的范围,

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


举例子来说明一下:


按照字节剪切
date | cut -b 1-5
2014
date | cut -b 1-6
2014
date | cut -b 1-7
2014年

从上面的例子可以看出汉字“年”占了3个字节


按照字符剪切

date
2014年 10月 10日 星期五 14:48:16 CST
date | cut -c 1,3,4
214

多个定位之间用逗号隔开


按照域剪切

date
2014年 10月 10日 星期五 14:46:09 CST
date | cut -d" " -f 4,5
星期五 14:46:12

-d指示分割符  并且分割符必须是单字符。