Gea*_*net 4 bash text-processing
我有一个包含三列的文件,我想按第三列获取重复的行,例如:
AAA = 342
BLABLABLA = 2
BBBx2 = 23
1+1 = 2
KOKOKO= 5
2x1 = 2
Run Code Online (Sandbox Code Playgroud)
输出应该是:
BLABLABLA = 2
1+1 = 2
2x1 = 2
Run Code Online (Sandbox Code Playgroud)
我尝试使用sort
and uniq
,但它删除了重复的行,我想打印它们。
只要您对最后一列感兴趣,就可以使用sort
and 来完成uniq
:
$ sort -k3n test.txt | uniq -f2 -D
1+1 = 2
2x1 = 2
BLABLABLA = 2
Run Code Online (Sandbox Code Playgroud)
在这里, sort 选项-k3n
使文件从第三个字段开始按数字顺序排序;选项uniq
是:
-f2 Skip the first two fields before checking for uniqueness
-D Print all the repeated lines
Run Code Online (Sandbox Code Playgroud)
不幸的是,您无法控制要检查唯一性的字段数。您可以使用-w
指定要检查的字符数,但这仅在您感兴趣的字段是固定宽度时才有帮助。
另外,请注意尾随空格。它将包含在要检查唯一性的文本中。