The order command is used to change the order of the fields in a CSV stream. You can also use this command to duplicate or remove fields from a stream. CSVfix now supports named fields as well as numbered ones, provided the input data contains a header record specifying the field names.

See also: pad, truncate, exclude. inter

Flag

Req'd?

Description

-f fieldlist

No

Specifies a comma-separated list of field indices identifying the order in which input fields will appear in the command output. Each field may appear zero or more times. If a field does not exist in the input, an empty string representing it is inserted in the output.

Fields can be specified as individual numbers or as ranges, so these are equivalent:

-f 1,2,3,4,5,10,17,16,15

-f 1:5,10,17:15

Ranges also work with other commands that expect a list of numeric field indexes.

-rf fieldlist

No

As for the -f flag, but indexes from the end of the CSV record, rather than the beginning. Only one of -f or -rf can be specified.

-fn names

No

Specifies a list of named fields. This works in the same way as the numbered ffields, but the CSV file(s) being ordered must contain a header specifying the field names. See the example below for more details.

Note that no CSVfix commands except order support the -fn flag directly. If you wish to use named fields, you must always use order as the initial command in a pipeline, to get the fields into a known numerical order.

One (and only one) of -f, -rf  or -fn must be specified.

-nc

No

If a field does not exist in the input, do not create it in the output.



The following example takes the forename (first field) and sex (third field) fields from the names.csv file and swaps their order:

csvfix order -f 3,1 data/names.csv

which produces:

"M","Charles"
"F","Jane"
"M","Herman"
"M","Flann"
"F","George"
"F","Virginia"
"M","Oscar"

This further example uses the file army.csv, which contains a header specifying the names of the fields, and then uses the names to order the fields. It also uses the -ifn flag to remove the field name header row from the output.

csvfix order -ifn -fn rank,name data/army.csv

which produces:

"sgt","jones"
"maj","smith"
"maj","black"
"pvt","white"
"pvt","pink"

To illustrate the use of the -nc flag, the following command run on sales_quarter.csv:

csvfix order -f 4:1 data/sales_quarter.csv

produces:

"","550","200","2000"
"233","200","178","2001"
"119","104","55","2002"
"","","77","2003"

but:

csvfix order -f 4:1 -nc data/sales_quarter.csv

produces:

"550","200","2000"
"233","200","178","2001"
"119","104","55","2002"
"77","2003"


Created with the Personal Edition of HelpNDoc: Full-featured EPub generator