Blog
Skip to end of metadata
Go to start of metadata

Default values of variables used in the formulas evaluated by csv-eval can now be specified by --default-values option. For instance,

$ ( echo a,10 ; echo b,20 ) | csv-eval --fields=,y "a=x+y" --default-values="x=1;y=2"
a,10,11
b,20,21

assigns default values to x and y. Since y is present in the input stream as specified by --fields, its default is ignored. On the other hand, x is not in the input stream and, therefore, its default value is used in the formula.

This capability is useful as it allows one to easily omit some fields without having to change the formulas where they are used, provided that the omitted fields have default values. For example, assume you want to write a script that multiplies a 3d vectors by a scalar, where the scalar may be either different for each vector or the same. Your implementation may look like following:

#!/bin/bash
if [[ -n "$1" ]] ; then fields="x,y,z" ; defaults="--default-values=scalar=$1"
else fields="x,y,z,scalar" ; fi
csv-eval --fields "$fields" $defaults "x1=x*scalar; y1=y*scalar; z1=z*scalar"
  • No labels