Search code examples
shellcsvkit

csvcut: error: unrecognized arguments: filename.csv


Trying to reorder columns in a csv.

This works (calling single column names):

$ csvcut -c "Node ID" amazon_categories_nodes.csv

and this works (calling multiple column indexes):

$ csvcut -c 4,1,3,5 amazon_categories_nodes.csv

But this doesn't work (calling multiple column names):

$ csvcut -c "Node ID", "Node Path" amazon_categories_nodes.csv
usage: csvcut [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
              [-p ESCAPECHAR] [-z FIELD_SIZE_LIMIT] [-e ENCODING] [-S] [-H]
              [-K SKIP_LINES] [-v] [-l] [--zero] [-V] [-n] [-c COLUMNS]
              [-C NOT_COLUMNS] [-x]
              [FILE]
csvcut: error: unrecognized arguments: amazon_categories_nodes.csv

Tried different combination of column names, also tried 3 or more column names but still receiving the above error message and not sure why? I expect the output to show the same csv but ordered in the way I've specified after -c


Solution

  • The -c flag of csvcut accepts a comma separated column names or indices ranges to be extracted. Your provided the column names right, but incorrectly included a space after the , as

    csvcut -c "Node ID", "Node Path" amazon_categories_nodes.csv
    #                  ^^^ (incorrect)
    

    Since the shell processes the command line arguments before passing it to the appropriate program, it interprets the argument value to -c as "Node ID", which is incorrect because a un-terminated sequence of comma separated values. Also it ends up leaving the "Node Path" value without any argument flags which csvcut doesn't understand.

    The right solution would be to not leave any space between

    csvcut -c "Node ID","Node Path" amazon_categories_nodes.csv