mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-19 09:24:14 +00:00
cut -o
This commit is contained in:
parent
a475308c0a
commit
b2b3535225
3 changed files with 41 additions and 44 deletions
|
|
@ -5,22 +5,18 @@
|
|||
#include "mapping/mappers.h"
|
||||
#include "cli/argparse.h"
|
||||
|
||||
#define DO_KEEP_REC_ORDER 0xe7
|
||||
#define DO_KEEP_ARG_ORDER 0xe8
|
||||
#define DO_COMPLEMENT 0xe9
|
||||
|
||||
typedef struct _mapper_cut_state_t {
|
||||
slls_t* pfield_name_list;
|
||||
hss_t* pfield_name_set;
|
||||
int do_which;
|
||||
int do_arg_order;
|
||||
int do_complement;
|
||||
} mapper_cut_state_t;
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
static sllv_t* mapper_cut_process(lrec_t* pinrec, context_t* pctx, void* pvstate) {
|
||||
if (pinrec != NULL) {
|
||||
mapper_cut_state_t* pstate = (mapper_cut_state_t*)pvstate;
|
||||
if (pstate->do_which == DO_KEEP_REC_ORDER) {
|
||||
|
||||
if (!pstate->do_complement) {
|
||||
// Loop over the record and free the fields not in the
|
||||
// to-be-retained set, being careful about the fact that we're
|
||||
// modifying what we're looping over.
|
||||
|
|
@ -33,32 +29,15 @@ static sllv_t* mapper_cut_process(lrec_t* pinrec, context_t* pctx, void* pvstate
|
|||
pe = pe->pnext;
|
||||
}
|
||||
}
|
||||
return sllv_single(pinrec);
|
||||
|
||||
} else if (pstate->do_which == DO_KEEP_ARG_ORDER) {
|
||||
|
||||
// Loop over the record and free the fields not in the
|
||||
// to-be-retained set, being careful about the fact that we're
|
||||
// modifying what we're looping over.
|
||||
for (lrece_t* pe = pinrec->phead; pe != NULL; /* next in loop */) {
|
||||
if (!hss_has(pstate->pfield_name_set, pe->key)) {
|
||||
lrece_t* pf = pe->pnext;
|
||||
lrec_remove(pinrec, pe->key);
|
||||
pe = pf;
|
||||
} else {
|
||||
pe = pe->pnext;
|
||||
if (pstate->do_arg_order) {
|
||||
// OK since the field-name list was reversed at construction time.
|
||||
for (sllse_t* pe = pstate->pfield_name_list->phead; pe != NULL; pe = pe->pnext) {
|
||||
char* field_name = pe->value;
|
||||
lrec_move_to_head(pinrec, field_name);
|
||||
}
|
||||
}
|
||||
|
||||
// OK since the field-name list was reversed at construction time.
|
||||
for (sllse_t* pe = pstate->pfield_name_list->phead; pe != NULL; pe = pe->pnext) {
|
||||
char* field_name = pe->value;
|
||||
lrec_move_to_head(pinrec, field_name);
|
||||
}
|
||||
|
||||
return sllv_single(pinrec);
|
||||
|
||||
} else { // DO_COMPLEMENT
|
||||
} else {
|
||||
for (sllse_t* pe = pstate->pfield_name_list->phead; pe != NULL; pe = pe->pnext) {
|
||||
char* field_name = pe->value;
|
||||
lrec_remove(pinrec, field_name);
|
||||
|
|
@ -80,7 +59,7 @@ static void mapper_cut_free(void* pvstate) {
|
|||
hss_free(pstate->pfield_name_set);
|
||||
}
|
||||
|
||||
static mapper_t* mapper_cut_alloc(slls_t* pfield_name_list, int do_which) {
|
||||
static mapper_t* mapper_cut_alloc(slls_t* pfield_name_list, int do_arg_order, int do_complement) {
|
||||
mapper_t* pmapper = mlr_malloc_or_die(sizeof(mapper_t));
|
||||
|
||||
mapper_cut_state_t* pstate = mlr_malloc_or_die(sizeof(mapper_cut_state_t));
|
||||
|
|
@ -89,7 +68,8 @@ static mapper_t* mapper_cut_alloc(slls_t* pfield_name_list, int do_which) {
|
|||
pstate->pfield_name_set = hss_alloc();
|
||||
for (sllse_t* pe = pfield_name_list->phead; pe != NULL; pe = pe->pnext)
|
||||
hss_add(pstate->pfield_name_set, pe->value);
|
||||
pstate->do_which = do_which;
|
||||
pstate->do_arg_order = do_arg_order;
|
||||
pstate->do_complement = do_complement;
|
||||
|
||||
pmapper->pvstate = (void*)pstate;
|
||||
pmapper->pprocess_func = mapper_cut_process;
|
||||
|
|
@ -111,14 +91,14 @@ static void mapper_cut_usage(char* argv0, char* verb) {
|
|||
// ----------------------------------------------------------------
|
||||
static mapper_t* mapper_cut_parse_cli(int* pargi, int argc, char** argv) {
|
||||
slls_t* pfield_name_list = NULL;
|
||||
int do_keep_arg_order = FALSE;
|
||||
int do_complement = FALSE;
|
||||
int do_arg_order = FALSE;
|
||||
int do_complement = FALSE;
|
||||
|
||||
char* verb = argv[(*pargi)++];
|
||||
|
||||
ap_state_t* pstate = ap_alloc();
|
||||
ap_define_string_list_flag(pstate, "-f", &pfield_name_list);
|
||||
ap_define_true_flag(pstate, "-o", &do_keep_arg_order);
|
||||
ap_define_true_flag(pstate, "-o", &do_arg_order);
|
||||
ap_define_true_flag(pstate, "-x", &do_complement);
|
||||
ap_define_true_flag(pstate, "--complement", &do_complement);
|
||||
|
||||
|
|
@ -132,8 +112,7 @@ static mapper_t* mapper_cut_parse_cli(int* pargi, int argc, char** argv) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int do_which = do_complement ? DO_COMPLEMENT : do_keep_arg_order ? DO_KEEP_ARG_ORDER : DO_KEEP_REC_ORDER;
|
||||
return mapper_cut_alloc(pfield_name_list, do_which);
|
||||
return mapper_cut_alloc(pfield_name_list, do_arg_order, do_complement);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -233,13 +233,14 @@ POKI_RUN_COMMAND{{mlr count-distinct -f a,b then sort -nr count data/medium}}HER
|
|||
|
||||
POKI_RUN_COMMAND{{mlr cut --help}}HERE
|
||||
|
||||
<p/>Note that <tt>cut</tt> doesn’t reorder field names — for that, use
|
||||
<a href="#reorder"><tt>reorder</tt></a>.
|
||||
|
||||
<table><tr><td>
|
||||
POKI_RUN_COMMAND{{mlr --opprint cat data/small}}HERE
|
||||
</td><td>
|
||||
POKI_RUN_COMMAND{{mlr --opprint cut -f y,x,i data/small}}HERE
|
||||
</td></tr><tr><td>
|
||||
POKI_RUN_COMMAND{{echo 'a=1,b=2,c=3' | mlr cut -f b,c,a}}HERE
|
||||
</td><td>
|
||||
POKI_RUN_COMMAND{{echo 'a=1,b=2,c=3' | mlr cut -o -f b,c,a}}HERE
|
||||
</td></tr></table>
|
||||
|
||||
<p/>
|
||||
|
|
|
|||
|
|
@ -573,16 +573,15 @@ a=eks,b=zee,count=357
|
|||
<pre>
|
||||
$ mlr cut --help
|
||||
Usage: mlr cut [options]
|
||||
-f {a,b,c} Field names to cut.
|
||||
-f {a,b,c} Field names to include for cut.
|
||||
-o Retain fields in the order specified here in the argument list.
|
||||
Default is to retain them in the order found in the input data.
|
||||
-x|--complement Exclude, rather that include, field names specified by -f.
|
||||
Passes through input records with specified fields included/excluded.
|
||||
</pre>
|
||||
</div>
|
||||
<p/>
|
||||
|
||||
<p/>Note that <tt>cut</tt> doesn’t reorder field names — for that, use
|
||||
<a href="#reorder"><tt>reorder</tt></a>.
|
||||
|
||||
<table><tr><td>
|
||||
<p/>
|
||||
<div class="pokipanel">
|
||||
|
|
@ -611,6 +610,24 @@ i x y
|
|||
</pre>
|
||||
</div>
|
||||
<p/>
|
||||
</td></tr><tr><td>
|
||||
<p/>
|
||||
<div class="pokipanel">
|
||||
<pre>
|
||||
$ echo 'a=1,b=2,c=3' | mlr cut -f b,c,a
|
||||
a=1,b=2,c=3
|
||||
</pre>
|
||||
</div>
|
||||
<p/>
|
||||
</td><td>
|
||||
<p/>
|
||||
<div class="pokipanel">
|
||||
<pre>
|
||||
$ echo 'a=1,b=2,c=3' | mlr cut -o -f b,c,a
|
||||
b=2,c=3,a=1
|
||||
</pre>
|
||||
</div>
|
||||
<p/>
|
||||
</td></tr></table>
|
||||
|
||||
<p/>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue