Greg Minshall <minsh...@acm.org> writes: > hi. i just wandered down a rathole others could avoid. the following > program fails in (what was to me) a mysterious way: > ---- > #+BEGIN_SRC awk :var a=2 > BEGIN{print $a;} > #+END_SRC > ---- > > it turns out values for variables to awk need to be strings (rather than > a number, as above). below is a patch to give what might be a less > mysterious error message. > > also: for an awk invocation with ":var a=b", all occurrences of "$a" in > the body of the awk code are changed to "b". i'm curious why this is > done rather than invoking awk with "-v a=b"? or, moving the > initialization into a "BEGIN{}" block? maybe because of the variety of > awk variants loose in the world? >
Hi Greg, Thanks for pointing out this shortcoming and for suggesting the command-line variable assignment option. I've just pushed up a patch which changes the behavior of awk code blocks to assign variables on the command line, so the following now work. #+begin_src awk :var a=2 BEGIN{ print a; } #+end_src #+RESULTS: : 2 Thanks, > > cheers, Greg > ---- > diff --git a/lisp/ob-awk.el b/lisp/ob-awk.el > index ed98afd..162ddfb 100644 > --- a/lisp/ob-awk.el > +++ b/lisp/ob-awk.el > @@ -47,6 +47,8 @@ > (defun org-babel-expand-body:awk (body params) > "Expand BODY according to PARAMS, return the expanded body." > (dolist (pair (mapcar #'cdr (org-babel-get-header params :var))) > + (if (not (stringp (cdr pair))) > + (error "awk variable values must be strings")) > (setf body (replace-regexp-in-string > (regexp-quote (format "$%s" (car pair))) (cdr pair) body))) > body) > -- Eric Schulte https://cs.unm.edu/~eschulte PGP: 0x614CA05D (see https://u.fsf.org/yw)