Add library of helper functions for creating/processing CSV files #127
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a
lib.awk
library with various helper functions, intended mainly for CSV processing and CSV file creation. The idea here is to try these out in pure AWK form (with fairly inefficient implementations!) and then move to native Go implementations later if they prove useful.printheader / printfields
The intention here is to allow you to create CSV files from scratch. Set
OFIELDS
and useprintheader()
inBEGIN
to set up and print the field names, then useprintfields(a)
to output the rows. I'm not sure whethersetfields()
should be "public" or not. Or maybe we could just havesetfields()
and to print you'd just saysetfields(a); print
. Theprintfields()
function is a bit handier though.Note: I'm still not convinced we need these functions at all, and would like to see real-world use cases where they actually make the code clearer or simpler. See the "Why not just this?" comments under the examples below.
Example usage, to create a 3-row CSV file (plus header row):
Or, to create a CSV file from a much larger input:
delfield, insfield, fieldnum
The intention with
delfield
andinsfield
is to allow you to easily delete or insert columns from many-columned CSV files where that is simpler than re-printing all the fields.I think the delete/insert one field would be the common case, hence the singular names, but it is a bit weird when you're using them to delete/insert multiple fields (maybe we should have both
delfield(n)
anddelfields(n, num)
? It's also arguably a bit unexpected that if you calleddelfield(n, 0)
orinsfield(n, 0)
it would actually delete/insert 1 field, not 0 (because of how AWK "default" arguments work).Both functions can be used with the
fieldnum()
helper that returns the number of a given field name fromFIELDS
.Examples:
Fixes #125.