emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] a minor patch to awk invocation


From: Eric Schulte
Subject: Re: [O] a minor patch to awk invocation
Date: Fri, 06 Jun 2014 12:52:21 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4.50 (gnu/linux)

Greg Minshall <address@hidden> 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)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]