从 UNIX shell 脚本中的列表中选择唯一或不同的值
我有一个 ksh 脚本,它返回一长串值,以换行符分隔,并且我只想查看唯一/不同的值。 有可能这样做吗?
例如,假设我的输出是目录中的文件后缀:
<前><代码>焦油 广州 爪哇 广州 爪哇 柏油 班级 班级
我想看到这样的列表:
<前><代码>焦油 广州 爪哇 班级
I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this?
For example, say my output is file suffixes in a directory:
tar gz java gz java tar class class
I want to see a list like:
tar gz java class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可能想查看
uniq
和sort
应用程序。(仅供参考,是的,在此命令行中进行排序是必要的,
uniq
仅删除紧随其后的重复行)编辑:
与 < 发布的内容相反a href="https://stackoverflow.com/questions/618378/select-unique-or-distinct-values-from-a-list-in-unix-shell-script/618382#618382">亚伦·迪古拉 与
uniq
的命令行选项相关:给定以下输入:
uniq
将输出所有行一次:uniq -d
将输出所有行出现多次的行,它将打印一次:uniq -u
将输出所有只出现一次的行,并将打印一次:You might want to look at the
uniq
andsort
applications.(FYI, yes, the sort is necessary in this command line,
uniq
only strips duplicate lines that are immediately after each other)EDIT:
Contrary to what has been posted by Aaron Digulla in relation to
uniq
's commandline options:Given the following input:
uniq
will output all lines exactly once:uniq -d
will output all lines that appear more than once, and it will print them once:uniq -u
will output all lines that appear exactly once, and it will print them once:这与一氧化碳答案,但更简洁一点。
This is the same as monoxide's answer, but a bit more concise.
使用 zsh 你可以这样做:
或者你可以使用 AWK:
With zsh you can do this:
Or you can use AWK:
使用 AWK 你可以做到:
我发现它比 sort 和 uniq 更快
With AWK you can do:
I find it faster than sort and uniq
通过
sort
和uniq
对它们进行管道传输。 这会删除所有重复项。uniq -d
仅给出重复项,uniq -u
仅给出唯一项(去除重复项)。Pipe them through
sort
anduniq
. This removes all duplicates.uniq -d
gives only the duplicates,uniq -u
gives only the unique ones (strips duplicates).对于可能不需要排序的较大数据集,您还可以使用以下 perl 脚本:
这基本上只是记住每一行输出,以便它不会再次输出。
与“
sort | uniq
”解决方案相比,它的优势在于无需预先进行排序。For larger data sets where sorting may not be desirable, you can also use the following perl script:
This basically just remembers every line output so that it doesn't output it again.
It has the advantage over the "
sort | uniq
" solution in that there's no sorting required up front.根据要求,唯一(但未排序);
使用更少的系统资源来处理少于约 70 个元素(经时间测试);
编写为从标准输入获取输入,
(或修改并包含在另一个脚本中):
(重击)
Unique, as requested, (but not sorted);
uses fewer system resources for less than ~70 elements (as tested with time);
written to take input from stdin,
(or modify and include in another script):
(Bash)
我得到了更好的提示来获取文件中的非重复条目
I get a better tips to get non-duplicate entries in a file