Implement nl -n format

This commit is contained in:
sin 2015-02-20 14:12:03 +00:00
parent b8fbaa9b0d
commit cd51795423
3 changed files with 37 additions and 3 deletions

2
README
View File

@ -48,7 +48,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=* mktemp non-posix none =* mktemp non-posix none
=* mv yes none (-i) =* mv yes none (-i)
=* nice yes none =* nice yes none
= nl no -d, -f, -h, -l, -n, -p = nl no -d, -f, -h, -l, -p
=* nohup yes none =* nohup yes none
#* paste yes none #* paste yes none
=* printenv non-posix none =* printenv non-posix none

18
nl.1
View File

@ -8,6 +8,7 @@
.Nm .Nm
.Op Fl b Ar type .Op Fl b Ar type
.Op Fl i Ar incr .Op Fl i Ar incr
.Op Fl n Ar format
.Op Fl s Ar sep .Op Fl s Ar sep
.Op Fl v Ar startnum .Op Fl v Ar startnum
.Op Fl w Ar width .Op Fl w Ar width
@ -40,6 +41,23 @@ a regular expression as defined in
.El .El
.It Fl i Ar incr .It Fl i Ar incr
Defines the increment between numbered lines. Defines the increment between numbered lines.
.It Fl n Ar format
Specify the line number output format.
The
.Ar format
can be any of the following:
.Bl -tag -width pstringXX
.It ln
Left justified.
.It rn
Right justified.
.It rz
Right justified with leading zeroes.
.El
.Pp
The default
.Ar format
is rn.
.It Fl s Ar sep .It Fl s Ar sep
Defines the string used to separate line numbers and lines. By default this is Defines the string used to separate line numbers and lines. By default this is
a tab. a tab.

20
nl.c
View File

@ -8,7 +8,12 @@
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
#define FORMAT_LN "%-*ld%s%s"
#define FORMAT_RN "%*ld%s%s"
#define FORMAT_RZ "%0*ld%s%s"
static char mode = 't'; static char mode = 't';
static const char *format = FORMAT_RN;
static const char *sep = "\t"; static const char *sep = "\t";
static int width = 6; static int width = 6;
static size_t startnum = 1; static size_t startnum = 1;
@ -25,7 +30,7 @@ nl(const char *name, FILE *fp)
if ((mode == 'a') if ((mode == 'a')
|| (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0)) || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
|| (mode == 't' && buf[0] != '\n')) { || (mode == 't' && buf[0] != '\n')) {
printf("%*ld%s%s", width, startnum, sep, buf); printf(format, width, startnum, sep, buf);
startnum += incr; startnum += incr;
} else { } else {
printf(" %s", buf); printf(" %s", buf);
@ -39,7 +44,7 @@ nl(const char *name, FILE *fp)
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-b type] [-i incr] [-s sep] [-v startnum] [-w width] [file]\n", argv0); eprintf("usage: %s [-b type] [-i incr] [-n format] [-s sep] [-v startnum] [-w width] [file]\n", argv0);
} }
int int
@ -60,6 +65,17 @@ main(int argc, char *argv[])
case 'i': case 'i':
incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX)); incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
break; break;
case 'n':
format = EARGF(usage());
if (!strcmp(format, "ln"))
format = FORMAT_LN;
else if (!strcmp(format, "rn"))
format = FORMAT_RN;
else if (!strcmp(format, "rz"))
format = FORMAT_RZ;
else
eprintf("%s: bad format\n", format);
break;
case 's': case 's':
sep = EARGF(usage()); sep = EARGF(usage());
break; break;