From b2b3535225eb75bfa9e1ce87d8dee508ce2eb739 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Tue, 26 May 2015 07:28:12 -0400 Subject: [PATCH] cut -o --- c/mapping/mapper_cut.c | 53 ++++++++++------------------------ doc/content-for-reference.html | 7 +++-- doc/reference.html | 25 +++++++++++++--- 3 files changed, 41 insertions(+), 44 deletions(-) diff --git a/c/mapping/mapper_cut.c b/c/mapping/mapper_cut.c index 44637dac7..f354ef8dc 100644 --- a/c/mapping/mapper_cut.c +++ b/c/mapping/mapper_cut.c @@ -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); } // ---------------------------------------------------------------- diff --git a/doc/content-for-reference.html b/doc/content-for-reference.html index 4bbc61a90..b87036a7a 100644 --- a/doc/content-for-reference.html +++ b/doc/content-for-reference.html @@ -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 -

Note that cut doesn’t reorder field names — for that, use -reorder. -
POKI_RUN_COMMAND{{mlr --opprint cat data/small}}HERE POKI_RUN_COMMAND{{mlr --opprint cut -f y,x,i data/small}}HERE +
+POKI_RUN_COMMAND{{echo 'a=1,b=2,c=3' | mlr cut -f b,c,a}}HERE + +POKI_RUN_COMMAND{{echo 'a=1,b=2,c=3' | mlr cut -o -f b,c,a}}HERE

diff --git a/doc/reference.html b/doc/reference.html index 44c586973..fd0495313 100644 --- a/doc/reference.html +++ b/doc/reference.html @@ -573,16 +573,15 @@ a=eks,b=zee,count=357

 $ 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.
 

-

Note that cut doesn’t reorder field names — for that, use -reorder. -

@@ -611,6 +610,24 @@ i x y

+

+

+

+
+$ echo 'a=1,b=2,c=3' | mlr cut -f b,c,a
+a=1,b=2,c=3
+
+
+

+

+

+

+
+$ echo 'a=1,b=2,c=3' | mlr cut -o -f b,c,a
+b=2,c=3,a=1
+
+
+