6

I'm dealing with a trivial shell script that invokes the mysql binary on a remote server, and having org-babel capture its results as a table.

With sensitive information scrubbed, the source block resembles

#+BEGIN_SRC sh :results value table
#!/bin/sh
ssh remote.host 'mysql db -e "SELECT foo,bar,baz FROM stuff"'
#+END_SRC

and its result is formatted as tab-separated values, the first row of which is a header naming the columns:

foo^Ibar^Ibaz
value^Ivalue^Ivalue
value^Ivalue^Ivalue
⋮

Org-babel correctly interprets this result as a table. What it doesn't do, and I haven't been able to figure out how to get it to do, is treat the first row of this result as a header. More specifically, I want there to be an hline automatically inserted after the first row, so that I can pass just the values to downstream code blocks, without having to have the code in them explicitly filter out the first line.

I'm (almost) sure there is a fairly trivial way to do this, because I've seen examples in other questions on Emacs SE of code block result tables which (appear to be) automatically formatted the way I want mine to be. I just haven't been able to find how to do it for myself in the org-babel documentation.

1 Answer 1

8

A rather brute force way is to define a post-processing function to add an hline after the first row.

#+name: addhdr
#+begin_src emacs-lisp :var tbl=""
(cons (car tbl) (cons 'hline (cdr tbl)))
#+end_src

Then call that function as postprocessing using the :post header:

#+begin_src elisp :results value table :post addhdr(*this*)
'((a b c d) (1 2 3 4) (a b c d) (1 2 3 4))
#+end_src

#+RESULTS:
| a | b | c | d |
|---+---+---+---|
| 1 | 2 | 3 | 4 |
| a | b | c | d |
| 1 | 2 | 3 | 4 |

Note that the addhdr function lacks checks for empty tables.

1
  • I can live with brute force as long as it works, and this does, quite well. Thanks! Jan 14, 2016 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.