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. |
-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. |
-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: Write eBooks for the Kindle