试图在我的系统上获取唯一的 shell 列表。当我运行此命令时:
cat /etc/passwd | cut -d ':' -f 7 | uniq
Run Code Online (Sandbox Code Playgroud)
我得到:
cat /etc/passwd | cut -d ':' -f 7 | uniq
Run Code Online (Sandbox Code Playgroud)
我不明白为什么uniq
不做我想做的事。我不明白什么?
我尝试获取此输出,制作副本,用副本中/bin/bash
的另一行替换其中一行,然后diff
对文件进行ing 却没有输出,所以我猜不是隐藏字符?
这是因为如何uniq
运作,来自男人:
注意:'uniq' 不会检测重复的行,除非它们是相邻的。您可能想先对输入进行排序,或者使用不带 'uniq' 的 'sort -u'。
所以,更好地使用,不需要cat
:
$ cut -d ':' -f 7 /etc/passwd | sort -u
/bin/bash
/bin/false
/bin/sync
/usr/sbin/nologin
Run Code Online (Sandbox Code Playgroud)
或者,一个命令
awk -F: '{ print $7 | "sort -u" }' /etc/passwd
Run Code Online (Sandbox Code Playgroud)
@kojiro 的建议:
awk -F: '!s[$7]++{print $7}' /etc/passwd
Run Code Online (Sandbox Code Playgroud)
您必须将排序的输出传递给以uniq
使其工作。
$ cat /etc/passwd | cut -d ':' -f 7 | sort | uniq
/bin/bash
/bin/sync
/sbin/halt
/sbin/shutdown
Run Code Online (Sandbox Code Playgroud)