bash uniq - 比较行时跳过最后 N 个字符/字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2258169/
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-17 21:40:29  来源:igfitidea点击:

uniq - skipping last N characters/fields when comparing lines

bashshell

提问by Juliusz

'man uniq' documents the -f=N and -s=N options which make uniq skip the first N fields/characters respectively when comparing lines, but how would you force uniq to skip the lastN fields/characters?

'man uniq' 记录了 -f=N 和 -s=N 选项,它们使 uniq 在比较行时分别跳过前 N 个字段/字符,但是如何强制 uniq 跳过最后N 个字段/字符?

回答by gaston

If you want the functionality of sorting first and then keeping only one line for each unique combination of the fields you are sorting on, you can make do with the unix utility sort alone.

如果您想要先排序的功能,然后为您排序的字段的每个唯一组合只保留一行,您可以单独使用 unix 实用程序 sort。

As an example, consider the following file, named some_data

例如,考虑以下文件,名为 some_data

a;c;4
a;b;9
a;b;6

We want to sort by the first and second field, and leave the third field alone, so we do a stable sort, like this:

我们想按第一个和第二个字段排序,不理会第三个字段,所以我们做一个稳定的排序,像这样:

$ sort -k1,1 -k2,2 -t';' --stable some_data

which gives

这使

a;b;9
a;b;6
a;c;4

Now say we'd like to keep only unique combinations of the first and second column. Then we'd do this:

现在假设我们只想保留第一列和第二列的唯一组合。然后我们会这样做:

$ sort -k1,1 -k2,2 -t';' --stable --unique some_data

which gives

这使

a;b;9
a;c;4

回答by ghostdog74

you will need to sort your data first if you want to use uniq

如果要使用 uniq,则需要先对数据进行排序

 sort file | rev | uniq -f 10 | rev

回答by ennuikiller

rev $filename | sort | uniq -f=N | rev

转 $ 文件名 | 排序 | uniq -f=N | 转