简体   繁体   English

如何在不以“特定字符”结尾的 bash 中显示所有用户

[英]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. compgenbash中的内置函数,通常用于用户名补全。

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.

相关问题 在 bash 中,为什么 which 显示错误的路径? - In bash, why does which show the wrong path? 如何使用bash从大文件中提取所有以特定字符开头的单词? - How to extract all words that start with specific character from large file using bash? 如何在bash中的特定字符之前获取字符串 - How to get a string before a specific character in bash 如何在 bash 命令行或脚本中位置更改的文件中替换特定字符? - How can I replace a specific character in a file where it's position changes in bash command line or script? 如何为所有用户设置密码(Bash Linux) - How to set a password for all users (Bash Linux) Bash / ZSH - 如何从特定行复制到文件末尾,然后粘贴到另一个文件的第 1 行,同时保留该文件中的所有文本 - Bash / ZSH - How to copy from a specific line to the end of a file, then paste at line 1 of another file whist preserving all text in that file 如何在dockers组中显示所有用户? - How to show all users in dockers group? 如何选择特定的行并在bash shell的末尾回显 - How to select specific lines and echo at the end in bash shell 在 BASH 中的字符串结尾之前插入一个字符 4 个字符 - Insert a character 4 chars before end of string in BASH 在 Linux 上显示不包含特定字符串的行 - Show lines which does not contain specific string on Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM