[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

uniq with sort-like "--key" support

From: Assaf Gordon
Subject: uniq with sort-like "--key" support
Date: Mon, 11 Feb 2013 20:31:41 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.4) Gecko/20120510 Icedove/10.0.4


I'd like to offer a proof-of-concept patch for adding sort-like "--key" support 
for the 'uniq' program, as discussed here:
and in several other threads.

The patch involves few core changes:
1. All key-related functions were copied as-is from "sort.c", and put in a 
separate file (uniq_sort_common.h). In theory, those could extracted later on 
to file that will be used by both sort and uniq. At the moment, it's a 
hodge-podge of copy&paste, including code that's not relevant to uniq (like 

2. The function "check_files" was modified to convert "struct linebuffer" (used 
by uniq) to "struct line" (used by sort's functions)

and then

3. The "different" function was modified to call sort's "keycompare" function.

4. In main(), the key argument passing was copied from 'sort', and some code 
was added to adapt previous options (e.g. skip-fields/skip-chars/check-chars) 
to internal "struct keyfield" .

The result is that uniq can now do:
$ printf "A 1\nA 2\nB 2\n" | ./src/uniq -k1,1
A 1
B 2
$ printf "A 1\nA 2\nB 2\n" | ./src/uniq -k2,2
A 1
A 2

Most (but not all) of the existing tests pass.
New tests to demonstrate the new possibilities have been added to 
'tests/misc/', try with:
 make check TESTS=tests/misc/uniq-key SUBDIRS=.

I think that most of the keycomparison functions (like 
numeric/general-numeric/month/version/skip-blanks) would "just work", though I 
haven't tested it thoroughly yet.

Comments are welcomed, 

Attachment: 0001-uniq-support-sort-like-key-specification.patch.xz
Description: application/xz

reply via email to

[Prev in Thread] Current Thread [Next in Thread]