Add an 'l' style for sorting that sorts by the string length of the field.

This commit is contained in:
christos 2010-12-18 23:09:48 +00:00
parent c732e7b752
commit 7e6e5c1f48
5 changed files with 51 additions and 33 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: fields.c,v 1.31 2009/11/06 18:34:22 joerg Exp $ */
/* $NetBSD: fields.c,v 1.32 2010/12/18 23:09:48 christos Exp $ */
/*-
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@ -65,7 +65,7 @@
#include "sort.h"
__RCSID("$NetBSD: fields.c,v 1.31 2009/11/06 18:34:22 joerg Exp $");
__RCSID("$NetBSD: fields.c,v 1.32 2010/12/18 23:09:48 christos Exp $");
#define SKIP_BLANKS(ptr) { \
if (BLANK & d_mask[*(ptr)]) \
@ -80,6 +80,7 @@ __RCSID("$NetBSD: fields.c,v 1.31 2009/11/06 18:34:22 joerg Exp $");
static u_char *enterfield(u_char *, const u_char *, struct field *, int);
static u_char *number(u_char *, const u_char *, u_char *, u_char *, int);
static u_char *length(u_char *, const u_char *, u_char *, u_char *, int);
#define DECIMAL_POINT '.'
@ -200,6 +201,8 @@ enterfield(u_char *tablepos, const u_char *endkey, struct field *cur_fld,
end = tcol.p->end;
}
if (flags & L)
return length(tablepos, endkey, start, end, flags);
if (flags & N)
return number(tablepos, endkey, start, end, flags);
@ -361,3 +364,14 @@ number(u_char *pos, const u_char *bufend, u_char *line, u_char *lineend,
return (last_nz_pos);
}
static u_char *
length(u_char *pos, const u_char *bufend, u_char *line, u_char *lineend,
int flag)
{
u_char buf[32];
int l;
SKIP_BLANKS(line);
l = snprintf((char *)buf, sizeof(buf), "%td", lineend - line);
return number(pos, bufend, buf, buf + l, flag);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: init.c,v 1.27 2010/06/06 00:00:33 wiz Exp $ */
/* $NetBSD: init.c,v 1.28 2010/12/18 23:09:48 christos Exp $ */
/*-
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@ -63,7 +63,7 @@
#include "sort.h"
__RCSID("$NetBSD: init.c,v 1.27 2010/06/06 00:00:33 wiz Exp $");
__RCSID("$NetBSD: init.c,v 1.28 2010/12/18 23:09:48 christos Exp $");
#include <ctype.h>
#include <string.h>
@ -223,17 +223,18 @@ int
optval(int desc, int tcolflag)
{
switch(desc) {
case 'b':
if (!tcolflag)
return (BI);
else
return (BT);
case 'd': return (D);
case 'f': return (F);
case 'i': return (I);
case 'n': return (N);
case 'r': return (R);
default: return (0);
case 'b':
if (!tcolflag)
return BI;
else
return BT;
case 'd': return D;
case 'f': return F;
case 'i': return I;
case 'l': return L;
case 'n': return N;
case 'r': return R;
default: return 0;
}
}

View File

@ -1,4 +1,4 @@
.\" $NetBSD: sort.1,v 1.30 2010/05/14 16:58:32 jruoho Exp $
.\" $NetBSD: sort.1,v 1.31 2010/12/18 23:09:48 christos Exp $
.\"
.\" Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
.\" All rights reserved.
@ -59,7 +59,7 @@
.\"
.\" @(#)sort.1 8.1 (Berkeley) 6/6/93
.\"
.Dd May 14, 2010
.Dd December 18, 2010
.Dt SORT 1
.Os
.Sh NAME
@ -67,7 +67,7 @@
.Nd sort or merge text files
.Sh SYNOPSIS
.Nm sort
.Op Fl bcdfHimnrSsu
.Op Fl bcdfHilmnrSsu
.Oo
.Fl k
.Ar field1 Ns Op Li \&, Ns Ar field2
@ -155,6 +155,8 @@ Considers all lowercase characters that have uppercase
equivalents to be the same for purposes of comparison.
.It Fl i
Ignore all non-printable characters.
.It Fl l
Sort by the string length of the field, not by the field itself.
.It Fl n
An initial numeric string, consisting of optional blank space, optional
minus sign, and zero or more digits (including decimal point)
@ -275,7 +277,7 @@ have the form
.Ar m Ns Li \&. Ns Ar n
and can be followed by one or more of the letters
.Cm b , d , f , i ,
.Cm n ,
.Cm l , n ,
and
.Cm r ,
which correspond to the options discussed above.

View File

@ -1,4 +1,4 @@
/* $NetBSD: sort.c,v 1.59 2010/06/05 17:44:51 dholland Exp $ */
/* $NetBSD: sort.c,v 1.60 2010/12/18 23:09:48 christos Exp $ */
/*-
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@ -76,7 +76,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
__RCSID("$NetBSD: sort.c,v 1.59 2010/06/05 17:44:51 dholland Exp $");
__RCSID("$NetBSD: sort.c,v 1.60 2010/12/18 23:09:48 christos Exp $");
#include <sys/types.h>
#include <sys/time.h>
@ -147,7 +147,7 @@ main(int argc, char *argv[])
fldtab = emalloc(fldtab_sz * sizeof(*fldtab));
memset(fldtab, 0, fldtab_sz * sizeof(*fldtab));
#define SORT_OPTS "bcdD:fik:mHno:rR:sSt:T:ux"
#define SORT_OPTS "bcdD:fHik:lmno:rR:sSt:T:ux"
/* Convert "+field" args to -f format */
fixit(&argc, argv, SORT_OPTS);
@ -167,7 +167,7 @@ main(int argc, char *argv[])
for (i = 0; optarg[i]; i++)
debug_flags |= 1 << (optarg[i] & 31);
break;
case 'd': case 'f': case 'i': case 'n':
case 'd': case 'f': case 'i': case 'n': case 'l':
fldtab[0].flags |= optval(ch, 0);
break;
case 'H':
@ -286,7 +286,7 @@ main(int argc, char *argv[])
if (fldtab[1].icol.num == 0) {
/* No sort key specified */
if (fldtab[0].flags & (I|D|F|N)) {
if (fldtab[0].flags & (I|D|F|N|L)) {
/* Modified - generate a key that covers the line */
fldtab[0].flags &= ~(BI|BT);
setfield("1", &fldtab[++fld_cnt], fldtab->flags);
@ -398,7 +398,7 @@ usage(const char *msg)
if (msg != NULL)
(void)fprintf(stderr, "%s: %s\n", getprogname(), msg);
(void)fprintf(stderr,
"usage: %s [-bcdfHimnrSsu] [-k field1[,field2]] [-o output]"
"usage: %s [-bcdfHilmnrSsu] [-k field1[,field2]] [-o output]"
" [-R char] [-T dir]", getprogname());
(void)fprintf(stderr,
" [-t char] [file ...]\n");

View File

@ -1,4 +1,4 @@
/* $NetBSD: sort.h,v 1.32 2010/06/05 17:44:51 dholland Exp $ */
/* $NetBSD: sort.h,v 1.33 2010/12/18 23:09:48 christos Exp $ */
/*-
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@ -78,13 +78,14 @@
/* values for masks, weights, and other flags. */
/* R and F get used to index weight_tables[] */
#define R 1 /* Field is reversed */
#define F 2 /* weight lower and upper case the same */
#define I 4 /* mask out non-printable characters */
#define D 8 /* sort alphanumeric characters only */
#define N 16 /* Field is a number */
#define BI 32 /* ignore blanks in icol */
#define BT 64 /* ignore blanks in tcol */
#define R 0x01 /* Field is reversed */
#define F 0x02 /* weight lower and upper case the same */
#define I 0x04 /* mask out non-printable characters */
#define D 0x08 /* sort alphanumeric characters only */
#define N 0x10 /* Field is a number */
#define BI 0x20 /* ignore blanks in icol */
#define BT 0x40 /* ignore blanks in tcol */
#define L 0x80 /* Sort by field length */
/* masks for delimiters: blanks, fields, and termination. */
#define BLANK 1 /* ' ', '\t'; '\n' if -R is invoked */