手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux cut命令学习笔记

[日期:2014-10-11] 来源:Linux社区  作者:chenlei0630 [字体: ]

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指示分割符  并且分割符必须是单字符。

10个有用的Linux命令面试问题及答案 http://www.linuxidc.com/Linux/2014-08/105150.htm

Linux命令之cd http://www.linuxidc.com/Linux/2013-05/83948.htm

Linux命令之cat http://www.linuxidc.com/Linux/2013-05/83947.htm

Linux命令之alias/unalias http://www.linuxidc.com/Linux/2013-05/83946.htm

Linux命令解析:su root和su -root http://www.linuxidc.com/Linux/2013-04/82564.htm

Linux命令之 交互式输入read http://www.linuxidc.com/Linux/2013-03/81594.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2014-10/107877.htm

linux
相关资讯       cut命令  Linux cut命令 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款