使用uniq命令过滤变量的内容

时间:2015-11-17 12:02:19

标签: bash shell unix

我有

variable =
avi
avi
mkw
cd
dvd

我试图用uniq =过滤它来删除重复项,我已经尝试过这样的

x=`printf "$variable" | tr "\n" "," | uniq -u`

我希望变量看起来像avi,mkw,cd,dvd but看起来像avi,avi,mkw,cd,dvd = uniq不能完成它的工作。对于shell来说,我很难搞糊涂,为什么uniq不工作?

2 个答案:

答案 0 :(得分:2)

def durationInMonth(d1=nil, d2=nil) d1 = d1 ? d1 : self.started_on d2 = d2 ? d2 : self.finished_on d1 = Date.strptime(d1, '%d.%m.%Y') d2 = Date.strptime(d2, '%d.%m.%Y') month_number = (d2.year * 12 + d2.month) - (d1.year * 12 + d1.month) month_number.divmod(12) return 1 if month_number == 0 return month_number + 1 # e.g. 06.11.15 and 10.12.15 = 1 + 1 end 要求对其输入进行排序。

uniq

但是,使用x=$(printf "$variable" | sort | uniq | tr "\n" ",") 后,您只需使用sort标记即可忽略对-u的调用:

uniq

答案 1 :(得分:0)

ls | cut -d "." -f2 | sort | uniq | tr "\n" ","