我需要在uniq -D选项中不支持的文件中打印所有重复的行。因此,我正在考虑另一种使用awk打印重复行的方法。我知道,我们在awk中有一个选择,如下所示。
testfile.txt
apple
apple
orange
orange
cherry
cherry
kiwi
strawberry
strawberry
papaya
cashew
cashew
pista命令:
awk 'seen[$0]++' testfile.txt但是上面只打印了唯一的重复行。我需要的输出与uniq -D命令检索的输出相同。
apple
apple
orange
orange
cherry
cherry
strawberry
strawberry
cashew
cashew发布于 2016-04-08 18:14:23
如果您只想使用普通的awk,您必须处理该文件两次:一次生成计数,一次消除计数等于1的行:
awk 'NR==FNR {count[$0]++; next} count[$0]>1' testfile.txt testfile.txt发布于 2016-04-08 19:22:59
不需要解析文件两次:
$ awk 'c[$0]++; c[$0]==2' file
apple
apple
orange
orange
cherry
cherry
strawberry
strawberry
cashew
cashew发布于 2016-04-09 04:01:33
使用sed:
$ sed 'N;/^\(.*\)\n\1$/p;$d;D' testfile.txt
apple
apple
orange
orange
cherry
cherry
strawberry
strawberry
cashew
cashew这样做的目的如下:
N # Append next line to pattern space
/^\(.*\)\n\1$/p # Print if lines in pattern space are identical
$d # Avoid printing lone non-duplicate last line
D # Delete first line in pattern space有几个限制:
https://stackoverflow.com/questions/36506212
复制相似问题