The printf command formats each row of CSV input using a format string similar to that used in the C printf function. For example, the following format string:

"%d --  %12s -- %3.8f"

formats the first CSV field as an integer, the second as a string left padded to 12 character positions and the third as a real number with 3 places before the decimal point and 5 after. Each field is separated by a pair of literal minus signs. Note that if the fields in the CSV data are not in the required order for the printf command, you can easily swap them around using the order command in a pipeline prior to printf.

If there are fields for which there is no corresponding formatter, they are not output. If there are formatters with no matching field values, the empty string is used. If a numeric conversion is attempted for a non-numeric field, the field is treated as if it contained numeric zero.

You can tell the command to ignore a particular field by using the non-standard %@ formatter. This behaves as if the field had been formatted with %s, but outputs nothing.

Note that the output of the printf command is not CSV, unless you explicitly format it to be so yourself, or use the -csv option.

See also: template, order

Flag

Req'd?

Description

-fmt format

Yes

Specifies the printf()-style format string to use to format the CSV data

-f order

No

Works exactly like the -f option of the order command, specifying an ordering of fields which will be used before the format specified by -fmt is applied. This is provided as a convenience, as it turns out that most uses of the printf command otherwise require input to be piped through the order command.

-q

No

Applies CSV-style quoting to any double-quotes in strings produced by printf formatters, but not to literal double-quotes. For example, a field containing Al "Scarface" Capone would be converted to Al ""Scarface"" Capone.

-csv

No

Make output be a single CSV record, which is appended to the current input row.



The following example prints the initial and surname of the authors in the names.csv file:

csvfix  printf -fmt "%1.1s. %s" data/names.csv

producing:

C. Dickens
J. Austen
H. Melville
F. O'Brien
G. Elliot
V. Woolf
O. Wilde

Created with the Personal Edition of HelpNDoc: What is a Help Authoring tool?