cut命令练习总结

cut命令的作用是实现文本内容的切割,原内容或者原文件不受影响。

  cut小练习:

          
            2016年 07月 21日 星期四 23:19:56 CST

            1:以字节为为单位进行切割 -b
                 date | cut -b 1-4
                     显示结果为 2016
                 date | cut -b 1-5
                     显示结果依然为2016

                 date | cut -b 1-6
                      显示结果依然为2016
                 date | cut -b 1-7
                      显示结果为2017年
                所以得出一个汉字占据三个字符

                  注意,当时用-b提取中文数据时候,可能会出现乱码的问题
                             对比如下
                                  [root@localhost ~]# date | cut -b 7
                                   ´
                                  [root@localhost ~]# date | cut -nb 7
                                   年
                               当遇到多字节字符时,可以使用-n选项,-n用于告诉cut不要将多字节字符拆开。

            2:以字符为单位进行切割-c

                 date | cut -c 1-4
                     显示结果为2016
                 date | cut -c 1-5
                      显示结果为2016年

                则一个汉字为一个字符

            3:以自定义分隔符进行切割-d

                  cat /etc/passwd | head -n 5 | cut -d: -f 1,3,5
                      显示结果为
                        root:0:root
                        bin:1:bin
                        daemon:2:daemon
                        adm:3:adm
                        lp:4:lp
                 
                 *当文件中存在制表符和空格符时候,怎么区分
                     首先查看cut.txt的文件内容
 
                     [root@localhost test]# cat cut.txt
                     this is tab    finish init
                     this is several space    finish

                     [root@localhost test]# sed -n l cut.txt
                     this is tab\tfinish init$
                     this is several space    finish$
                     
                     可以看出第一行tab后面\t,这就是制表符,而空格符依然以空格显示,用cut命令操作看看有什么不同
                     [root@localhost test]# cat cut.txt | cut -d ' ' -f 3
                     tab    finish
                     several
                     结果显示虽然是空格,但是却是制表符显示的结果,换个方式显示
                     [root@localhost test]# cat cut.txt | cut -d ' ' -f -3
                     this is tab    finish
                     this is several
                     其中tab    finish中间是用制表符隔开的
                     -3代表from first to M'th (included) byte, character or field 从第一个到3中间中间所有符合的。

原创文章,作者:我的滑板鞋,如若转载,请注明出处:http://www.178linux.com/25141

(1)
我的滑板鞋我的滑板鞋
上一篇 2016-07-22 10:06
下一篇 2016-07-22 10:06

相关推荐

  • 马哥教育网络19期+第十八周课程练习

    1、为LNMP架构添加memcached支持,并完成对缓存效果的测试报告; LNMP的安装过程不再赘述. # yum install -y memcached # cat /etc/sysconfig/memcached  PORT="11211" &nbsp…

    Linux干货 2016-09-19
  • N22-第一周作业

    1、描述计算机的组成及其功能    组成:        硬件:            CPU:运算器、控制器、寄存器、缓存     &nb…

    Linux干货 2016-08-15
  • 路径别名的配置

    创建bbs目录,在里面创建html文件 [root@bluee logs]# cd /www/htdocs [root@bluee htdocs]# ls index.html [root@bluee htdocs]# mkdir bbs [root@bluee htdocs]# vim bbs/index.html [root@bluee htdocs]#…

    Linux干货 2016-08-05
  • 马哥教育N22期第七周作业

    1、创建一个10G分区,并格式为ext4文件系统; [root@localhost xuc]# cat /proc/partitions  major minor  #blocks  name    8   &…

    Linux干货 2016-10-24
  • Python from entry to abandon 3

        第十章的内容是解决问题————编写一个Python脚本。在我的电脑上因为Zip命令不能正常工作所以无法给出演示。该章给出了很有意义的编程思路,对以后学习和工作都有不错的参考意义,这部分有兴趣的同学还是自己去看原版教程吧。    这篇博客结合个人笔记整理了《简明Python教程》第十一章到第十…

    Linux干货 2017-04-07
  • 第三周作业

    博客具体内容请移步博客园: http://www.cnblogs.com/ITOps/p/6204549.html

    Linux干货 2016-12-20