【每天一个linux命令】cut

参考:【linux之cut用法】http://www.cnblogs.com/dong008259/archive/2011/12/09/2282679.html

【简介】

cut:截取一段数据的指定数据

【命令】

cut  [-bn] [file] 或 cut [-c] [file]  或  cut [-df] [file]

【常用参数】

-b :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。
-c :以字符为单位进行分割。
-d :自定义分隔符,默认为制表符。
-f :与-d一起使用,指定显示哪个区域。
-n :取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的<br />范围之内,该字符将被写出;否则,该字符将被排除。

# b字节,c字符
# 星期一
[service@ndl-bass-ys-vm-129-186 wy]$ cat test.txt |cut -b 3

[service@ndl-bass-ys-vm-129-186 wy]$ cat test.txt |cut -c 3# 每行第2、4个字节
[service@ndl-bass-ys-vm-129-186 wy]$ who |cut -c 2,4
ev

# 每行第 2、3、4、6 个字符(2-4指 2、3、4)
[service@ndl-bass-ys-vm-129-186 wy]$ who |cut -c 2-4,6
ervc

# 每行从第1~3个字符(包括第3个)
[service@ndl-bass-ys-vm-129-186 wy]$ who |cut -b -3
ser

# 每行从第3个往后的字符(包括第3个)
[service@ndl-bass-ys-vm-129-186 wy]$ who |cut -b 3-
rvice  pts/0        2017-11-08 16:12 (172.18.252.72)

# -d : 以:分割域
# -f 1,2 指定显示第1、2个域
# root:x:0:0:root:/root:/bin/bash
[service@ndl-bass-ys-vm-129-186 wy]$ cat /etc/passwd |cut -d : -f 1,2
root:x

 

posted @ 2017-11-08 16:56  白小白001  阅读(220)  评论(0编辑  收藏  举报