截取/etc/passwd文件中的用户名,有的时候很有作用。

先介绍一个命令: cut

命令格式

 
  
  1. cut -b list [-n] [file...] 
  2. cut -c list [file...] 
  3. cut -f list [-d delim] [-s] [file...] 

-b  表示字节 (byte)
-c 表示字符 (char)
-f 表示字段 (field)
-d  表示分割夫 默认为tab
-n  具体的数字
list 表示范围 范围是从1开始 -b1-3表示 从1到3个字节

用cut取出/etc/passwd中的所有用户名:

 

 
  
  1. cut -d: -f 1 /etc/passwd #说明:分隔符为":" ,取第一个字段 
  2. cut -c1-8 /etc/passwd    #说明:截取每行的第1到第8个字 

还有一种方法是使用awk,方法如下:

 

 
  
  1. awk 'BEGIN{FS=":"}{print $1}' < /etc/passwd 

    当然还有其他的方法。