文章目录
1. 简述
cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。
cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。
2.基本用法
cut [选项参数] filename
说明:默认分隔符是制表符
2.1 选项参数说明
选项参数 功能
-f 列号,提取第几列
-d 分隔符,按照指定分隔符分割列
-b 按照字节分割
2.2 案列
数据准备
[root@Centos-root473791 ~]# touch cut.txt
[root@Centos-root473791 ~]# vi cut.txt
[root@Centos-root473791 ~]# cat cut.txt
haha hehe
dada dada
ee ee
ff ff
gg gg
[root@Centos-root473791 ~]#
2.2.1 切割每一行第 1-4 个字节
[root@Centos-root473791 ~]# cat cut.txt
haha hehe
dada dada
ee ee
ff ff
gg gg
[root@Centos-root473791 ~]# cut -b 1-4 cut.txt
haha
dada
ee
ff
gg
[root@Centos-root473791 ~]#
2.2.2 切割cut.txt第一列
[root@Centos-root473791 ~]# cat cut.txt
haha hehe
dada dada
ee ee
ff ff
gg gg
[root@Centos-root473791 ~]# cut -d " " -f 1 cut.txt
haha
dada
ee
ff
gg
[root@Centos-root473791 ~]#
2.2.3 切割cut.txt第二、三列
[root@Centos-root473791 ~]# cat cut.txt
haha hehe
dada dada
ee ee
ff ff
gg gg
[root@Centos-root473791 ~]# cut -d " " -f 2,3 cut.txt
hehe
dada
[root@Centos-root473791 ~]#
2.2.4 在cut.txt文件中切割出第一个gg
[root@Centos-root473791 ~]# cat cut.txt
haha hehe
dada dada
ee ee
ff ff
gg gg
[root@Centos-root473791 ~]# cat cut.txt | grep "gg" | cut -d " " -f 1
gg
2.2.5 选取系统PATH变量值,第2个“:”开始后的所有路径:
[root@Centos-root473791 ~]# echo $PATH
/usr/local/src/zookeeper/apache-zookeeper-3.6.2-bin/bin:/usr/local/src/java/jdk1.8.0_181/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@Centos-root473791 ~]# echo $PATH | cut -d: -f 2-
/usr/local/src/java/jdk1.8.0_181/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@Centos-root473791 ~]#
2.2.6 切割IP地址
[root@Centos-root473791 ~]# ifconfig eth0 | grep "inet addr" | cut -d: -f 2 | cut -d" " -f1
192.168.2.133
[root@Centos-root473791 ~]#