简体   繁体   中英

uniq - skipping last N characters/fields when comparing lines

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

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.

As an example, consider the following file, named 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

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

 sort file | rev | uniq -f 10 | rev

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM