11

I try to pipe the output of one source block to the next source block as standard input. Here an example of what I have so far:

Create stdin data:
#+header: :exports code
#+header: :results output
#+begin_src sh
echo "That goes to the next"
#+end_src

#+name: piped
#+RESULTS:
: That goes to the next 

Use "piped" as stdin:
#+header: :exports results
#+header: :stdin piped
#+header: :results output
#+begin_src sh
VALUE=$(cat)
echo "I got:"
echo "$VALUE"
#+end_src

My problems with this are:

  • I have to manually create the result of the first block by hitting C-c C-c

  • the result must be included in org-buffer (large output not needed otherwise)

  • the result must be named manually

Is there a workaround or better way to do this?

10

Here's a simple way to fix your code by naming the src block instead of results:

#+name: piped
#+header: :exports code
#+header: :results output
#+begin_src sh
echo "That goes to the next"
#+end_src

#+RESULTS:
: That goes to the next 

#+header: :exports results
#+header: :stdin piped
#+header: :results output
#+begin_src sh
VALUE=$(cat)
echo "I got:"
echo "$VALUE"
#+end_src

#+results:
: I got:
: That goes to the next
  • 1
    Very good, thanks, that really helped. – theldoria Jun 28 '16 at 14:37
3

I had a similar use case, and rolled a simple exporter which let me use json-mode for the source/input from stdin:

;;; ob-passthrough.el ---  passthrough evaluator          -*- lexical-binding: t; -*-

;; this ob evaluates the block as ifself, so it can be used as input
;; for another block

(require 'ob)

(defun org-babel-execute:passthrough (body params)
  body)

;; json output is json
(defalias 'org-babel-execute:json 'org-babel-execute:passthrough)

(provide 'ob-passthrough)
;;; ob-passthrough.el ends here

Then, add (passthrough . t) to org-babel-list-langauges, and here it is in action:

#+NAME: json-test
#+BEGIN_SRC json
  {"greet": "hello, world"}
#+END_SRC

#+HEADER: :stdin json-test
#+BEGIN_SRC sh
  jq .greet
#+END_SRC

#+RESULTS:
: hello, world
2

Call a src block from another using "noweb" references (see (info "(org) Noweb reference syntax")):

#+name: input
#+header: :exports code
#+header: :results output
#+begin_src sh
echo "That goes to the next"
#+end_src

#+header: :exports results
#+header: :results output :noweb no-export
#+begin_src sh
VALUE=$(<<input>>)
echo "I got:"
echo "$VALUE"
#+end_src

#+results:
: I got:
: That goes to the next
  • 1
    That is cool, good to know, thanks. Unfortunately the second source code block really has to use stdin. The use of cat in the shell was only a simple example. – theldoria Jun 28 '16 at 14:36
0

Another way to solve this problem is to name the input as an EXAMPLE or a QUOTE block if the input is truly static. Something like this:

#+NAME: some-json
#+BEGIN_QUOTE
{"label": "Hello json"}
#+END_QUOTE

or an EXAMPLE if you prefer:

#+NAME: some-json-2
#+BEGIN_EXAMPLE
{"label": "ehlo json"}
#+END_EXAMPLE

then reference those named blocks in the code you wish to evaluate; here we use the QUOTE example:

#+NAME: the-code
#+HEADER: :stdin some-json
#+BEGIN_SRC shell
jq .label
#+END_SRC

Since the value of the some-json block is static, there is no need to evaluate it. Evaluating the-code block gives:

#+RESULTS: the-code
: Hello json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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