Skip to content
/ csv2tpl Public

Generate repeated text from a CSV file using a text template

License

Notifications You must be signed in to change notification settings

rsubr/csv2tpl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

csv2tpl

Use Case

This application is a "command generator" which can be used to quickly generate CLI commands for various purposes.

The author has used this to generate:

  1. Power Shell commands to move VMWare VMs into vCenter folders
  2. Power Shell commands to bulk create Active Directory users
  3. Create static DHCP leases on a Cisco switch
  4. Generate boilerplate PHP configuration code

Download the latest version from the releases page.

Usage

./csv2tpl
Usage: csv2tpl [-verbose] [-noempty] template-file csv-file

Simple Example

To configure static DHCP leases on a Cisco switch:

  1. Create a CSV file with the required fields dhcp-pool.csv
  2. Create a Cisco IOS CLI template refering the fields, see cisco-dhcp-cli.tpl
  3. Run csv2tpl to create the Cisco CLI commands
  4. Paste the csv2tpl output into the Cisco CLI
# Template file
$ cat examples/cisco-dhcp-cli.tpl
ip dhcp pool host {{.hostname}}
address {{.ip}} {{.netmask}} hardware-address {{.macid}}
dns-server {{.dns1}} {{.dns2}}

# Associated CSV file
$ cat examples/dhcp-pool.csv
hostname,ip,netmask,macid,dns1,dns2
abe,10.0.0.10,255.255.255.0,00:50:56:80:00:10,10.10.10.10,20.20.20.20
ace,10.0.0.11,255.255.255.0,00:50:56:80:00:11,10.10.10.10,20.20.20.20
amy,10.0.0.12,255.255.255.0,00:50:56:80:00:12,10.10.10.10,20.20.20.20
...

# Output generated by this application
$ ./csv2tpl examples/cisco-dhcp-cli.tpl examples/dhcp-pool.csv

ip dhcp pool host abe
address 10.0.0.10 255.255.255.0 hardware-address 00:50:56:80:00:00
dns-server 10.10.10.10 20.20.20.20
ip dhcp pool host ace
address 10.0.0.11 255.255.255.0 hardware-address 00:50:56:80:00:01
dns-server 10.10.10.10 20.20.20.20
ip dhcp pool host amy
address 10.0.0.12 255.255.255.0 hardware-address 00:50:56:80:00:02
dns-server 10.10.10.10 20.20.20.20
...

Advanced Usage

The template file is a standard golang text/template and all the templating features can be used.

The template variables names are taken from the CSV header and every row is passed to the template as a string map.

Beginning and ending white space in CSV header and data rows are trimmed.

If the -noempty flag is set, then the application aborts when there are any empty CSV columns, all rows and columns must have data.

If using Excel, the CSV must be saved in CSV (Comma delimited) (*.csv) format and NOT in CSV UTF-8 (Comma delimited) (*.csv). Excel adds UTF BOM to the start of the CSV file which is unsupported by the Go's text/template.

Older Python Version

The first version of this application was written in Python and can be found in the python folder. A Windows 64bit release can be found in the v1.0 releases page. This version is archived and no longer developed as the Go version has far better templating features.

References

  1. Go text/template reference

About

Generate repeated text from a CSV file using a text template

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published