在UNIX shell脚本中的列表中选择唯一的或不同的值 [英] Select unique or distinct values from a list in UNIX shell script
问题描述
我有一个的ksh脚本返回值的一个长长的清单,换行符分开了,我只想看到独特的/不同的值。这是可能做到这一点?
举例来说,说我的输出目录中的文件后缀:
焦油
GZ
java的
GZ
java的
柏油
类
类
块引用>我想看到这样一个列表:
焦油
GZ
java的
类
块引用>解决方案您可能想看看
uniq的
和排序
应用程序。
./yourscript.ksh |排序| uniq的(仅供参考,是的,那种在这个命令行必要的,
uniq的
只去掉重复的行,经过对方会立即)编辑:
相反的是已发布的<一个href=\"http://stackoverflow.com/questions/618378/select-unique-or-distinct-values-from-a-list-in-unix-shell-script/618382#618382\">Aaron Digulla 关于
uniq的
的命令行选项:由于以下输入:
类
罐
罐
罐
箱子
箱子
java的
uniq的
将输出所有行恰好一次:
类
罐
箱子
java的
uniq的-d
将输出出现不止一次,它会一次全部打印出来行:
罐
箱子
的uniq -u
将输出恰好出现一次的所有行,它将打印一次他们:
类
java的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
解决方案You might want to look at the
uniq
andsort
applications../yourscript.ksh | sort | uniq(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:
class jar jar jar bin bin java
uniq
will output all lines exactly once:class jar bin java
uniq -d
will output all lines that appear more than once, and it will print them once:jar bin
uniq -u
will output all lines that appear exactly once, and it will print them once:class java
这篇关于在UNIX shell脚本中的列表中选择唯一的或不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!