码迷,mamicode.com
首页 > 系统相关 > 详细

linux命令学习之cut

时间:2014-10-10 18:15:54      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:linux   cut   

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

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


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

  1. 按字节剪切:-b(byte)
  2. 按字符剪切:-c(character)
  3. 按域剪切    :-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指示分割符  并且分割符必须是单字符。


参考:http://www.cnblogs.com/xianghang123/archive/2011/12/06/2277531.html

linux命令学习之cut

标签:linux   cut   

原文地址:http://blog.csdn.net/chenlei0630/article/details/39961445

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!