cut #取列 awk命令的小弟 默认的分隔符为tab键
选项:
-d #指定分隔符
-f #取出指定的列
-c #取出指定的字符 按照行进行处理的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
[root@qls ~]# echo "root:x:0:0:root:/root:/bin/bash" >test.txt [root@qls ~]# cat test.txt root:x:0:0:root:/root:/bin/bash #取出第七列 [root@qls ~]# cut -d ":" -f7 test.txt /bin/bash #取出第一列和第七列 [root@qls ~]# cut -d ":" -f1,7 test.txt root:/bin/bash #取出第五列到第七列 [root@qls ~]# cut -d ":" -f5-7 test.txt root:/root:/bin/bash [root@qls ~]# cut -d ":" -f7 passwd /bin/bash /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /bin/sync /sbin/shutdown /sbin/halt /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin [root@qls ~]# cut -d ":" -f7 passwd | sort /bin/bash /bin/sync /sbin/halt /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/shutdown [root@qls ~]# cut -d ":" -f7 passwd | sort | uniq /bin/bash /bin/sync /sbin/halt /sbin/nologin /sbin/shutdown [root@qls ~]# cut -d ":" -f7 passwd | sort | uniq -c 1 /bin/bash 1 /bin/sync 1 /sbin/halt 14 /sbin/nologin 1 /sbin/shutdown [root@qls ~]# cut -d ":" -f7 passwd | sort | uniq -c | sort -n 1 /bin/bash 1 /bin/sync 1 /sbin/halt 1 /sbin/shutdown 14 /sbin/nologin [root@qls ~]# cut -d ":" -f7 passwd | sort | uniq -c | sort -rn 14 /sbin/nologin 1 /sbin/shutdown 1 /sbin/halt 1 /bin/sync 1 /bin/bash [root@qls ~]# cat test.txt root:x:0:0:root:/root:/bin/bash [root@qls ~]# cut -c 6 test.txt x [root@qls ~]# cut -c 6 passwd x : n : 4 x o x x t #取出不同的字符 [root@qls ~]# cut -c 6,8 test.txt x0 #取出连续的字符 [root@qls ~]# cut -c 1-4 test.txt root #取出系统eth0的IP地址 [root@qls ~]# yum install -y net-tools [root@qls ~]# ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::3310:9d15:9ee4:43e8 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:eb:ea:8d txqueuelen 1000 (Ethernet) RX packets 1634 bytes 464970 (454.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1033 bytes 114568 (111.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@qls ~]# ifconfig eth0 | head -2 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255 [root@qls ~]# ifconfig eth0 | head -2 | tail -1 inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255 [root@qls ~]# ifconfig eth0 | head -2 | tail -1 | cut -d " " -f10 10.0.0.100 [root@qls ~]# ip a s eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::3310:9d15:9ee4:43e8/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@qls ~]# ip a s eth0 | head -3 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@qls ~]# ip a s eth0 | head -3 | tail -1 inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@qls ~]# ip a s eth0 | head -3 | tail -1 | cut -c 10-19 10.0.0.100 [root@qls ~]# ip a s eth0 | head -3 | tail -1 inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@qls ~]# ip a s eth0 | head -3 | tail -1 | cut -d " " -f6 10.0.0.100/24 [root@qls ~]# ip a s eth0 | head -3 | tail -1 | cut -d " " -f6 | cut -d "/" -f1 10.0.0.100 |