使用cut命令和选择的组合来显示文件中每行的指定范围的列
语法:cut [OPTION]... [FILE]...
-f:显示指定列或指定范围的咧
-d:列之间的分隔符(默认是tab)
-c:显示每行的第几个字符到第几个字符
1、使用-f显示每行的第二列(分隔符是tab)
[root@localhost ~]# cat test.txt
first 192.168.0.1 NO.301
first 192.168.0.1 NO.301
first 192.168.0.1 NO.301
first 192.168.0.1 NO.301
first 192.168.0.1 NO.301
first 192.168.0.1 NO.301
first 192.168.0.1 NO.301
first 192.168.0.1 NO.301
[root@localhost ~]# cut -f2 test.txt
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
2、使用-f和-d显示每行的第二列(分隔符是逗号)
[root@localhost ~]# cat test.txt
first,192.168.0.1,NO.301
first,192.168.0.1,NO.301
first,192.168.0.1,NO.301
first,192.168.0.1,NO.301
first,192.168.0.1,NO.301
first,192.168.0.1,NO.301
first,192.168.0.1,NO.301
first,192.168.0.1,NO.301
//-d后跟着的分隔符必须和文件名中间有空格
[root@localhost ~]# cut -f2 -d,test.txt
cut: the delimiter must be a single character
Try `cut --help' for more information.
//正确
[root@localhost ~]# cut -f2 -d, test.txt
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
3、使用-c显示每行指定区间的字符
[root@localhost ~]# cut -c3-21 test.txt
rst,192.168.0.1,NO.
rst,192.168.0.1,NO.
rst,192.168.0.1,NO.
rst,192.168.0.1,NO.
rst,192.168.0.1,NO.
rst,192.168.0.1,NO.
rst,192.168.0.1,NO.
rst,192.168.0.1,NO.