Bash:grep 独特的线条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27598133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:03:10  来源:igfitidea点击:

Bash : grep unique lines

bashgrepuniq

提问by Lazuardi N Putra

I'd like to grepunique line. This is the file content:

我想要grep独特的路线。这是文件内容:

this is line 1
this is line 1
this is line 2
this is line 1
this is line 1

I just want to output this is line 2to my shell. How can I do that?

我只想输出this is line 2到我的外壳。我怎样才能做到这一点?

回答by KeepCalmAndCarryOn

sort x.txt |uniq -u

assuming your file is in x.txt which gives

假设您的文件在 x.txt 中,它给出

this is line 2 

回答by KeepCalmAndCarryOn

Check out man uniq:

退房man uniq

 -u      Only output lines that are not repeated in the input.

With that in mind, sort does a pairwise comparison to see if it's neighbor matches, meaning that you need to sort your output before feeding it to uniq:

考虑到这一点, sort 会进行成对比较以查看它的邻居是否匹配,这意味着您需要在将输出提供给 之前对其进行排序uniq

$ sort my_list.txt | uniq -u

$ sort my_list.txt | uniq -u

回答by Jotne

Here is an awksolution:

这是一个awk解决方案:

awk '{a[##代码##]++} END {for (i in a) if (a[i]==1) print i}' file
this is line 2