在UNIX shell脚本中的列表中选择唯一的或不同的值 [英] Select unique or distinct values from a list in UNIX shell script

查看:178
本文介绍了在UNIX shell脚本中的列表中选择唯一的或不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的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 and sort 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆