Skip to content

Commit 6948c2a

Browse files
committed
add new -count option
this makes it possible to use jobflow to cut a subset of line numbers like a combination of `head` and `tail` utilities. e.g. seq 100 | jobflow -skip=10 -count=10 will print the lines from 11-20
1 parent bd3b854 commit 6948c2a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

jobflow.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ typedef struct {
146146
unsigned long numthreads;
147147
unsigned long threads_running;
148148
unsigned long skip;
149+
unsigned long count;
149150
unsigned long delayedspinup_interval; /* use a random delay until the queue gets filled for the first time.
150151
the top value in ms can be supplied via a command line switch.
151152
this option makes only sense if the interval is somewhat smaller than the
@@ -371,13 +372,15 @@ static int syntax(void) {
371372
"until EOF is received. we call this 'pipe mode'.\n"
372373
"\n"
373374
"available options:\n\n"
374-
"-skip N -threads N -resume -statefile=/tmp/state -delayedflush\n"
375+
"-skip N -count N -threads N -resume -statefile=/tmp/state -delayedflush\n"
375376
"-delayedspinup N -buffered -joinoutput -limits mem=16M,cpu=10\n"
376377
"-eof=XXX\n"
377378
"-exec ./mycommand {}\n"
378379
"\n"
379380
"-skip N\n"
380381
" N=number of entries to skip\n"
382+
"-count N\n"
383+
" N=only process count lines (after skipping)\n"
381384
"-threads N (alternative: -j N)\n"
382385
" N=number of parallel processes to spawn\n"
383386
"-resume\n"
@@ -453,6 +456,7 @@ static int parse_args(unsigned argc, char** argv) {
453456
{"statefile", 0, 's', .dest.s = &prog_state.statefile },
454457
{"eof", 0, 's', .dest.s = &prog_state.eof_marker },
455458
{"skip", 0, 'i', .dest.i = &prog_state.skip },
459+
{"count", 0, 'i', .dest.i = &prog_state.count },
456460
{"resume", 0, 'b', .dest.b = &resume },
457461
{"delayedflush", 0, 'b', .dest.b = &prog_state.delayedflush },
458462
{"delayedspinup", 0, 'i', .dest.i = &prog_state.delayedspinup_interval },
@@ -463,6 +467,7 @@ static int parse_args(unsigned argc, char** argv) {
463467
};
464468

465469
prog_state.numthreads = 1;
470+
prog_state.count = -1UL;
466471

467472
for(i=1; i<argc; ++i) {
468473
char *p = argv[i], *q = strchr(p, '=');
@@ -671,7 +676,8 @@ static char* mystrnrchr_chk(const char *in, int ch, size_t end) {
671676
}
672677

673678
static int need_linecounter(void) {
674-
return !!prog_state.skip || prog_state.statefile || prog_state.use_seqnr;
679+
return !!prog_state.skip || prog_state.statefile ||
680+
prog_state.use_seqnr || prog_state.count != -1UL;
675681
}
676682
static size_t count_linefeeds(const char *buf, size_t len) {
677683
const char *p = buf, *e = buf+len;
@@ -723,7 +729,11 @@ static int dispatch_line(char* inbuf, size_t len, char** argv) {
723729
}
724730
if(!len) return 1;
725731
}
732+
} else if(prog_state.count != -1UL) {
733+
if(!prog_state.count) return -1;
734+
--prog_state.count;
726735
}
736+
727737
if(!prog_state.cmd_startarg) {
728738
write_all(1, inbuf, len);
729739
return 1;

test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ $JF -skip=5 < $(tmp).1 > $(tmp).2
6868
tail -n 5 < $(tmp).1 > $(tmp).3
6969
test_equal $(tmp).2 $(tmp).3
7070

71+
dotest "seq 10 catmode skip 5 count 3"
72+
seq 10 > $(tmp).1
73+
$JF -skip=5 -count=3 < $(tmp).1 > $(tmp).2
74+
tail -n 5 < $(tmp).1 | head -n 3 > $(tmp).3
75+
test_equal $(tmp).2 $(tmp).3
76+
7177
dotest "seq 10000 bulk skip 1337"
7278
seq 10000 | sort -u > $(tmp).1
7379
$JF -bulk=4K -skip=1337 -exec tests/stdin_printer.out < $(tmp).1 | sort -u > $(tmp).2

0 commit comments

Comments
 (0)