学习linux命令之:cut命令

命令解释:

通俗来说,就是把一段话,用这个命令来截取一分部或几部分。取出的部分显示在屏幕个。(个人理解)

命令用法:

cut 选项 文件 

常用参数:

              -b 按照字节来分

              -c  按照字符来分割

             -d   按照指定的字符来分割

              -f   按域来分割,通常和-d使用

下面分别用案例来介绍以上常用参数

           例:已知a.txt中有一段话 "i an chenlei my qq is 12345678"请把“chenlei 12345678”拿出来显示在屏幕上。用cut命令

 

[root@moban ~]# echo "i an chenlei my qq is 12345678" > a.txt
[root@moban ~]# cat a.txt
i an chenlei my qq is 12345678
[root@moban ~]# cut -b 6-12,23- a.txt
chenlei12345678
[root@moban ~]# cut -b 6-13,23- a.txt
chenlei 12345678

说明:这个是按照字节来分的,-b 

 

         例:已知a.txt中有一段话 "i an chenlei my qq is 12345678"请把“chenlei 12345678”拿出来显示在屏幕上。用cut -d命令

[root@moban ~]# cut -d " " -f3,7 a.txt
chenlei 12345678

说明:这个是按指定的分割符来分的,分割符为空格

 

         例:已知a.txt中有一段话 "i an chenlei my qq is 12345678"请把“chenlei 12345678”拿出来显示在屏幕上。用cut -c命令

[root@moban ~]# cut -c 6- a.txt|sed s#" my qq is "#" "#g
chenlei 12345678
说明:这个是-c和-b用法差不多,都是以字符为分割符,我是加了一个sed来替换。

 

        cut命令是用来分割的,可以和grep sed awk同时使用,可以完成很多无法完成的事

posted @ 2016-04-15 15:32  单机才牛  阅读(154)  评论(0编辑  收藏  举报