Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uniq - skipping last N characters/fields when comparing lines

Tags:

bash

shell

'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 last N fields/characters?

like image 700
Juliusz Avatar asked Feb 13 '10 16:02

Juliusz


1 Answers

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
like image 184
gaston Avatar answered Sep 30 '22 06:09

gaston