列出Linux中的用户
如何按字母顺序列出所有用户,以及前10个查看用户ID信息的用户(第5列)。列出Linux中的用户,linux,unix,Linux,Unix,如何按字母顺序列出所有用户,以及前10个查看用户ID信息的用户(第5列)。 我知道对于问题的第一部分,我可以使用像cat/etc/passwd | cut-d':'-f1,但是第二部分呢?我应该使用“for”或“if”之类的词吗?用grep删除标题并使用cut: cat /etc/passwd | grep -v '^#' | cut -d: -f1,5 使用GNU awk: awk -F: '{ users[cnt++]=$1 # Set up an arr
我知道对于问题的第一部分,我可以使用像
cat/etc/passwd | cut-d':'-f1
,但是第二部分呢?我应该使用“for”或“if”之类的词吗?用grep
删除标题并使用cut
:
cat /etc/passwd | grep -v '^#' | cut -d: -f1,5
使用GNU awk:
awk -F: '{
users[cnt++]=$1 # Set up an array with the users (first : separated field) as the value
}
END {
PROCINFO["sorted_in"]="@val_str_asc"; Set the array sort order to value ascending
for ( i in users ) { # Loop through each entry in the array
if ( cnt1 <= 4) { # Only print the first 5
print users[i];
cnt1++ # Use to track number of prints
}
}
}' /etc/passwd
awk-F:'{
users[cnt++]=$1#设置一个以用户(第一个:分隔字段)为值的数组
}
结束{
PROCINFO[“sorted_in”]=“@val_str_asc”;将数组排序顺序设置为升序
对于(用户中的i){#循环遍历数组中的每个条目
如果(cnt1
awk -F: '{ users[cnt++]=$1 } END { PROCINFO["sorted_in"]="@val_str_asc";for ( i in users ) { if ( cnt1 <= 4) { print users[i];cnt1++ } } }' /etc/passwd