The template command passes its output through a textual template specified in a template file. Place holders in the template are replaced by values from the CSV data.
Template files look like this:
This is the first value {1}
And this {2} is the second
where {1} and {2} are place holders that will be replaced by the values of fields 1 and 2 in the command's input. If you need to output literal braces, escape them with backslashes.
Place holders can also specify expressions. To do this, place an @ character after the opening brace:
This is the first value {1}
And this {2} is the second
And this is a random number {@random()}
When using expressions, the field numbers must be preceded by the $ sign:
This is the first and second values concatenated: {@$1 . $2}
Normally, all of the output of the template command will be directed to standard output (or to a single file, if you use the -o option). However, it can be desirable to direct the output of each individual input CSV record to a separate file. To do this, you use the -fn option, which takes a string as a parameter. This string is itself treated as a template. So, if you have the the CSV data in a file called heroes.csv:
Batman,Batcave
Superman,Fortress Of Solitude
and a template file called lairs.txt that looks like this:
{1} hangs out in the {2}
then the command:
csvfix template -tf lairs.txt -fn output/{1}_lair.txt heroes.csv
will produce two output files in the output directory:
Batman_lair.txt
Superman_lair.txt
The first will contain:
Batman hangs out in the Batcave
and the second:
Superman hangs out in the Fortress Of Solitude
Note that the output of a template command is not CSV, unless you explicitly make it so in the template specification.
See also: printf
Flag |
Req'd? |
Description |
-tf tfile |
Yes |
Name of template file. |
-fn ftpl |
No |
Specifies a string containing a template which will be used to generate file names under which template output will be saved. If this option is used, each CSV input record generates an output file with the name specified by the template. Important: No check is made for existing files, which will be overwritten! |
The following example lists the names.csv file using a template. The template file (names.tpl) looks like this:
Name: {1} {2}
Sex: {3}
The command line:
csvfix.exe template -tf data/names.tpl data/names.csv
produces:
Name: Charles Dickens
Sex: M
Name: Jane Austen
Sex: F
Name: Herman Melville
Sex: M
Name: Flann O'Brien
Sex: M
Name: George Elliot
Sex: F
Name: Virginia Woolf
Sex: F
Name: Oscar Wilde
Sex: M
Created with the Personal Edition of HelpNDoc: Easily create Help documents