From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: org babel support for tcl and awk Date: Tue, 24 May 2011 13:03:41 -0600 Message-ID: <87vcwz9b02.fsf@gmail.com> References: <20110524113109.fo2dcd0mwno0c4w4@webmail.dds.nl> <8739k46z2p.fsf@gmail.com> <874o4kvvcb.fsf@ucl.ac.uk> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([140.186.70.92]:34418) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOwtR-00056n-MB for emacs-orgmode@gnu.org; Tue, 24 May 2011 15:03:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QOwtP-00049Z-Lk for emacs-orgmode@gnu.org; Tue, 24 May 2011 15:03:49 -0400 Received: from mail-px0-f171.google.com ([209.85.212.171]:63471) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOwtP-000494-7U for emacs-orgmode@gnu.org; Tue, 24 May 2011 15:03:47 -0400 Received: by pxi7 with SMTP id 7so4693293pxi.30 for ; Tue, 24 May 2011 12:03:46 -0700 (PDT) In-Reply-To: <874o4kvvcb.fsf@ucl.ac.uk> (Eric S. Fraga's message of "Tue, 24 May 2011 18:53:24 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Eric Schulte Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Eric S Fraga writes: > Eric Schulte writes: > > [...] > >> As an example, I've worked up an very simple ob-awk.el file from >> ob-template.el, it is attached along with an example org-mode file which >> demonstrates its usage. > > Eric, > > this is great to see as I use awk quite often. What is involved in > extending this to be able to run an awk script on input from within the > org file (output of another babel block, for instance, as my typical use > of awk is to re-arrange output from another program...)? Or, if you > wish, can you suggest one of the ob-XXX modules that best illustrates > how to do this and I can give it a try? > I've made a quick change so that any variable named "stdin" is treated specially, in that, rather than using its value to replace strings of $stdin in the text of the awk code, the value of the stdin variable is saved into the file processed by awk. This allows awk to operate over Org-mode references. See the attached example file. If babel code block supported a pipe or an actual stdin header argument, that would be the ideal way to add this behavior, but currently nothing of that nature exists. Please let me know if this misses part of your suggestion, or more generally what else may be advisable before we add this to the core. Cheers -- Eric --=-=-= Content-Type: text/x-org Content-Disposition: attachment; filename=example.org * awk example #+results: simple-table | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | #+begin_src awk :var stdin=simple-table {print $1} #+end_src #+results: : 1 : 4 : 7 #+results: simple-text : Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do : eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad : minim veniam, quis nostrud exercitation ullamco laboris nisi ut : aliquip ex ea commodo consequat. Duis aute irure dolor in : reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla : pariatur. Excepteur sint occaecat cupidatat non proident, sunt in : culpa qui officia deserunt mollit anim id est laborum. --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=ob-awk.el Content-Transfer-Encoding: quoted-printable ;;; ob-awk.el --- org-babel functions for awk evaluation ;; Copyright (C) Eric Schulte ;; Author: Eric Schulte ;; Keywords: literate programming, reproducible research, awk ;; Homepage: http://orgmode.org ;; Version: 0.01 ;;; License: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;; This file is not intended to ever be loaded by org-babel, rather it ;; is a awk for use in adding new language support to Org-babel. ;; Good first steps are to copy this file to a file named by the ;; language you are adding, and then use `query-replace' to replace ;; all strings of "awk" in this file with the name of your new ;; language. ;; ;; If you have questions as to any of the portions of the file defined ;; below please look to existing language support for guidance. ;; ;; If you are planning on adding a language to org-babel we would ask ;; that if possible you fill out the FSF copyright assignment form ;; available at http://orgmode.org/request-assign-future.txt as this ;; will make it possible to include your language support in the core ;; of Org-mode, otherwise unassigned language support files can still ;; be included in the contrib/ directory of the Org-mode repository. ;;; Requirements: ;; Use this section to list the requirements of this language. Most ;; languages will require that at least the language be installed on ;; the user's system, and the Emacs major mode relevant to the ;; language be installed as well. ;;; Code: (require 'ob) (require 'ob-eval) (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk")) (defvar org-babel-awk-command "awk" "Name of the awk executable command.") (defun org-babel-expand-body:awk (body params &optional processed-params) "Expand BODY according to PARAMS, return the expanded body." (dolist (pair (remove-if (lambda (pair) (eql 'stdin (car pair))) (mapcar #'cdr (org-babel-get-header params :var)= ))) (setf body (replace-regexp-in-string (regexp-quote (concat "$" (car pair))) (cdr pair) body))) body) (defun org-babel-execute:awk (body params) "Execute a block of Awk code with org-babel. This function is called by `org-babel-execute-src-block'" (message "executing Awk source code block") (let* ((full-body (org-babel-expand-body:awk body params)) (code-file ((lambda (file) (with-temp-file file (insert full-body)= ) file) (org-babel-temp-file "awk-"))) (cmd-line (or (cdr (assoc :cmd-line params)) "")) (in-file (cdr (assoc :in-file params))) (vars (mapcar #'cdr (org-babel-get-header params :var))) (cmd (format "%s -f %s %s %s" org-babel-awk-command code-file cmd-line (or in-file ;; input to the awk script in-file or std= in var (when (assoc 'stdin vars) (let ((tmp (org-babel-temp-file "awk-stdin-"))) (with-temp-file tmp (insert (org-babel-awk-var-to-awk (cdr (assoc 'stdin vars))))) tmp)))))) (message "$ " cmd) (org-babel-awk-table-or-string (org-babel-eval cmd "")))) (defun org-babel-awk-var-to-awk (var &optional sep) "Return a printed value of VAR suitable for parsing with awk." (flet ((echo-var (v) (if (stringp v) v (format "%S" v)))) (cond ((and (listp var) (listp (car var))) (orgtbl-to-generic var (list :sep (or sep "\t") :fmt #'echo-var))) ((listp var) (mapconcat #'echo-var var "\n")) (t (echo-var var))))) (defun org-babel-awk-table-or-string (results) "If the results look like a table, then convert them into an Emacs-lisp table, otherwise return the results as a string." (org-babel-script-escape results)) (provide 'ob-awk) ;;; ob-awk.el ends here --=-=-= Content-Type: text/plain > > Thanks, > eric -- Eric Schulte http://cs.unm.edu/~eschulte/ --=-=-=--