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

linux 命令cut用法

时间:2016-07-07 22:40:13      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:linux cut

linux 命令cut用法

cut命令
        cut [OPTION]... [FILE]...
            -d DELIMITER: 指明分隔符
            -f FILEDS:
                #: 第#个字段
                #,#[,#]离散的多个字段例如1,3,6
                #-#连续的多个字段, 例如1-6

                混合使用1-3,7
            --output-delimiter=STRING

            [root@master ~]# cut -d :  -f 1 /etc/passwd
            root
            bin
            daemon
            adm
            lp
            sync
            shutdown
            halt
            mail
            uucp
            operator
            games
            gopher
            ftp
            nobody
            dbus
            usbmuxd
            vcsa
            rtkit
            avahi-autoipd
            abrt
            haldaemon
            gdm
            ntp
            apache
            saslauth
            postfix
            pulse
            sshd
            tcpdump
            hadoop
            zhangshan

            

            [root@master ~]# cut -d :  -f 1,3 /etc/passwd
            root:0
            bin:1
            daemon:2
            adm:3
            lp:4
            sync:5
            shutdown:6
            halt:7
            mail:8
            uucp:10
            operator:11
            games:12
            gopher:13
            ftp:14
            nobody:99
            dbus:81
            usbmuxd:113
            vcsa:69
            rtkit:499
            avahi-autoipd:170
            abrt:173
            haldaemon:68
            gdm:42
            ntp:38
            apache:48
            saslauth:498
            postfix:89
            pulse:497
            sshd:74
            tcpdump:72
            hadoop:500
            zhangshan:501

用法
-b 以字节为单位进行分割。这些字节位置将忽略多字节字符边界除非也指定了 -n 标志。
-c 以字符为单位进行分割。
-d 自定义分隔符默认为制表符。
-f  与-d一起使用指定显示哪个区域。
-n 取消分割多字节字符。

后面详细讲解
(1) -b 的用法
取每一行的第三个字节

[root@master ~]# who
root     tty2         2016-07-07 06:08
root     tty1         2016-07-07 06:06 (:0)
root     pts/0        2016-07-07 06:07 (:0.0)
root     pts/1        2016-07-07 06:08 (192.168.0.110)
[root@master ~]# who | cut -b 3
o
o
o
o

我想提取第1第2、第3和第4个字节怎么办?    

[root@master ~]# who|cut -b 1-3,4
root
root
root
root

取前3个字节 用-3

[root@master ~]# who|cut -b -3
roo
roo
roo
roo

取后3个字节 用3-

[root@master ~]# who|cut -b 3-
ot     tty2         2016-07-07 06:08
ot     tty1         2016-07-07 06:06 (:0)
ot     pts/0        2016-07-07 06:07 (:0.0)
ot     pts/1        2016-07-07 06:08 (192.168.0.110)

(2)-c的用法

取第1第2、第3和第4个字节这里和-b没有什么区别。

[root@master ~]# who|cut -c 1-3,4
root
root
root
root

所以用-b和-c没有区别如果你提取中文区别就看出来了来看看中文提取的情况

[root@master ~]# cat sb.txt 
今天是星期一我要上班。
今天是星期二我要上班。
今天是星期三我要上班。
今天是星期四我要上班。
今天是星期五我要上班。
今天是星期六我不用上班。
[root@master ~]# cut -b 3 sb.txt 
 
 
 
 
 
[root@master ~]# cut -c 3 sb.txt 
是
是
是
是
是
是

看到了吧用-c则会以字符为单位输出正常而-b只会傻傻的以字节8位二进制位来计算输出就是乱码。
(3)-n用法
当遇到多字节字符时可以使用-n选项-n用于告诉cut不要将多字节字符拆开。例子如下

下面命令没有结果

[root@master ~]# cut -nb 2  sb.txt


或者

下面命令没有结果

[root@master ~]# cat sb.txt | cut -nb 2

有结果

[root@master ~]# cat sb.txt | cut -nb 1,2,3
今
今
今
今
今
今

(4) -f的用法
/etc/passwd的前五行内容为例

[root@master ~]# cat /etc/passwd|head -n 5
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@master ~]# cat /etc/passwd|head -n 5|cut -d : -f 1
root
bin
daemon
adm
lp

看到了吧用-d来设置间隔符为冒号然后用-f来设置我要取的是第一个域。

下面另外一个例子

[root@master ~]# cat /etc/passwd|head -n 5|cut -d : -f 1-3,7
root:x:0:/bin/bash
bin:x:1:/sbin/nologin
daemon:x:2:/sbin/nologin
adm:x:3:/sbin/nologin
lp:x:4:/sbin/nologin

取前面3个

[root@master ~]# cat /etc/passwd|head -n 5|cut -d : -f -3
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4

(4)其他-自定义分隔符 -d
如果遇到空格和制表符时怎么分辨呢我觉得有点乱怎么办(这里有点问题。)

[root@master ~]# cat aa.txt 
this is a word
this is a word
this is a    word
this     is a word

    

[root@master ~]#  cat aa.txt |cut -d ‘ ‘ -f 3
a
a
a    word
a


希望大家指点共同进步哈哈。

本文出自 “梁小明的博客” 博客,请务必保留此出处http://7038006.blog.51cto.com/7028006/1812434

linux 命令cut用法

标签:linux cut

原文地址:http://7038006.blog.51cto.com/7028006/1812434

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