[英]How to show all users in bash which does not end with “specific character”s
All the users in the system
that dosen't have as an ending character on their names a , s , t , r , m , z must be shown in Bash. system
中所有名称中没有结尾字符的用户a , s , t , r , m , z必须显示在 Bash 中。
The users names can be obtained from the /etc/passwd
file, in the first column.用户名可以从
/etc/passwd
文件的第一列中获得。 But I can not perceive the correct approach to exclude those characters from the search.但我无法感知从搜索中排除这些字符的正确方法。
Should I use grep?我应该使用 grep 吗? Or just a cut?
还是只是一个切口?
Something like就像是
grep -o '^[^:]*[^astrmz:]:' /etc/passwd | tr -d :
or或者
cut -d: -f1 /etc/passwd | grep '[^astrmz]$'
[^blah]
matches any character but the ones listed, the opposite of [blah]
. [^blah]
匹配除列出的字符之外的任何字符,与[blah]
相反。
GNU grep
using a lookahead : GNU
grep
使用前瞻:
grep -Po '^[^:]*[^astrmz:](?=:)' /etc/passwd
Or using awk
instead:或者使用
awk
代替:
awk -F: '$1 ~ /[^astrmz]$/ { print $1 }' /etc/passwd
Or in pure bash
without external commands:或者在没有外部命令的纯
bash
中:
while IFS=: read -r name rest; do
if [[ $name =~ [^astrmz]$ ]]; then
echo "$name"
fi
done < /etc/passwd
As you can see, there's lots of potential approaches.如您所见,有很多潜在的方法。
Simple one liner when using bash
:使用
bash
时简单的一个衬垫:
compgen -u | grep -v '[astrmz]$'
The compgen -u
command will produce a list of users (without all of the extra fields present in /etc/passwd
); compgen -u
命令将产生一个用户列表(没有/etc/passwd
中存在的所有额外字段); compgen
is a builtin in bash
where it's normally used for username completion. compgen
是bash
中的内置函数,通常用于用户名补全。
This should do the trick:这应该可以解决问题:
cut -d: -f1 /etc/passwd | grep -vE 's$|t$|r$|m$|z$'
The cut command strips out the username from the password file. cut 命令从密码文件中删除用户名。 Then grep -v (does the UNMATCHING) grep -E does multiple matching (OR OR OR) the $ sign indicates the last character to match
然后 grep -v (不匹配) grep -E 进行多重匹配(OR OR OR) $ 符号表示要匹配的最后一个字符
For example, on my mac, I get:例如,在我的 Mac 上,我得到:
_gamecontrollerd
_ondemand
_wwwproxy
_findmydevice
_ctkd
_applepay
_hidd
_analyticsd
_fpsd
_timed
_reportmemoryexception
(You see no names end with those 5 letters). (您会看到没有以这 5 个字母结尾的名称)。
Good Luck.祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.