Uniq 不会删除重复项

Mat*_*ler 16 command-line curl uniq

我正在使用以下命令

curl -silent http://api.openstreetmap.org/api/0.6/relation/2919627 http://api.openstreetmap.org/api/0.6/relation/2919628 | grep node | awk '{print $3}' | uniq
Run Code Online (Sandbox Code Playgroud)

当我想知道为什么uniq不删除重复项时。知道为什么吗?

slm*_*slm 22

您必须对输出进行排序,以便uniq命令能够工作。请参阅手册页:

过滤来自 INPUT(或标准输入)的相邻匹配行,写入 OUTPUT(或标准输出)。

因此,您可以sort先将输出通过管道传输到其中,然后再通过管道传输uniq。或者您可以利用sort's 的能力来执行排序和唯一性,如下所示:

$ ...your command... | sort -u
Run Code Online (Sandbox Code Playgroud)

例子

排序 | 优衣库

$ cat <(seq 5) <(seq 5) | sort | uniq
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)

排序 -u

$ cat <(seq 5) <(seq 5) | sort -u
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)

你的榜样

$ curl -silent http://api.openstreetmap.org/api/0.6/relation/2919627 http://api.openstreetmap.org/api/0.6/relation/2919628 \
      | grep node | awk '{print $3}' | sort -u
ref="1828989762"
ref="1829038636"
ref="1829656128"
ref="1865479751"
ref="451116245"
ref="451237910"
ref="451237911"
ref="451237917"
ref="451237920"
ref="451237925"
ref="451237933"
ref="451237934"
ref="451237941"
ref="451237943"
ref="451237945"
ref="451237947"
ref="451237950"
ref="451237953"
Run Code Online (Sandbox Code Playgroud)