Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

7.6. cut - remove sections from each line of files

7.6.1. 字符操作

截取第 8 个字符

			
[root@production ~]# echo 1234567890 | cut -c 8
8
			
			

从第五个字符开始,截取后面的

			
[root@production ~]# echo 1234567890 | cut -c 5-
567890
			
			

截取第8个字符前面的字符串

			
[root@production ~]# echo 1234567890 | cut -c -8
12345678
			
			

从第4字符开始,截取至第8个字符为止

			
[root@production ~]# echo 1234567890 | cut -c 4-8
45678
			
			

每一行操作,截取位置1-4的字符

			
$ cat /etc/passwd | cut -c 1-4
root
daem
bin:
sys:
sync
game
man:

$ echo "No such file or directory"| cut -c4-7
such

$ echo "No such file or directory"| cut -c -8
No such

$ echo "No such file or directory"| cut -c-8
No such

        	
        	

7.6.2. 列操作

获取文本第一列

$ last | grep  'neo' | cut -d ' ' -f1
        
$ cat /etc/passwd | cut -d ':' -f1
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy

$ cat /etc/passwd | cut -d ':' -f1,3,4

# cat /etc/passwd | cut -d ':' -f1,6
root:/root
bin:/bin
daemon:/sbin
adm:/var/adm
lp:/var/spool/lpd
sync:/sbin
shutdown:/sbin
halt:/sbin
mail:/var/spool/mail
uucp:/var/spool/uucp
operator:/root
games:/usr/games
gopher:/var/gopher
ftp:/var/ftp
nobody:/
vcsa:/dev
saslauth:/var/empty/saslauth
postfix:/var/spool/postfix
sshd:/var/empty/sshd
rpc:/var/cache/rpcbind
rpcuser:/var/lib/nfs
nfsnobody:/var/lib/nfs
ntp:/etc/ntp
nagios:/var/log/nagios