cut命令是一个选取命令,其功能是将文件中的每一行”字节” ”字符” ”字段” 进行剪切,选取我们需要的
上面的-b -c 只是针对于格式固定的数据中剪切,但是对于一些格式不固定的,就没有办法获取到我们想要的数据,因此便有了 -f 域的概念,使用 -d 命令,指定其分割符为 :
[root@localhost shell]# cat /etc/passwd | head -n 3 | cut -d : -f 1
root
bin
daemon
[root@localhost shell]#
剪切ip地址,如下:
[root@localhost shell]# ifconfig eth0 | grep "inet addr"
inet addr:192.168.1.199 Bcast:192.168.1.255 Mask:255.255.255.0
[root@localhost shell]# ifconfig eth0 | grep "inet addr" | cut -d : -f 2
192.168.1.199 Bcast //以 : 为分隔符,选取第二个域里面的内容,输出
[root@localhost shell]# ifconfig eth0 | grep "inet addr" | cut -d : -f 2 | cut -d ' ' -f 1
192.168.1.199 //以空格为分割符,选取第一个域内的内容,输出
[root@localhost shell]#