1

I have a code chunk in my document that I want to use as a list of patterns to search for with grep.

If I were just writing a script without org-babel, I could write.

a="ar
er
ir
or
ur"

printf "Four\nscore\nand\nseven\nyears\nago" | grep -F "$a"

And the results would be

Four
score
years

But instead of $a, I want to use the output of an earlier code block.

#+NAME: vowel_r
#+BEGIN_SRC sh :shebang "#!/bin/bash" :results verbatim
a="ar
er
ir
or
ur"
echo "$a"
#+END_SRC

#+RESULTS: vowel_r
: ar
: er
: ir
: or
: ur

I would have thought that this would work.

#+BEGIN_SRC sh :shebang "#!/bin/bash" :noweb yes
printf "Four\nscore\nand\nseven\nyears\nago" | grep -F "<<vowel_r()>>"
#+END_SRC

But it throws the error.

/tmp/babel-4438QgK/sh-script-4438Wst: line 8: unexpected EOF while looking for matching `"'
/tmp/babel-4438QgK/sh-script-4438Wst: line 9: syntax error: unexpected end of file

By looking at the code with C-c C-v v, it seems that noweb is treating each line of the output separately, instead of as a multi-line string.

printf "Four\nscore\nand\nseven\nyears\nago" | grep -F "ar
printf "Four\nscore\nand\nseven\nyears\nago" | grep -F "er
printf "Four\nscore\nand\nseven\nyears\nago" | grep -F "ir
printf "Four\nscore\nand\nseven\nyears\nago" | grep -F "or
printf "Four\nscore\nand\nseven\nyears\nago" | grep -F "ur
printf "Four\nscore\nand\nseven\nyears\nago" | grep -F ""

Removing the quote marks around <<vowel_r>> just leads to a grep error.

How can I get noweb to treat the output of the earlier block as a multi-line string, instead of handling each line separately?

Thank you!

0

I don't use noweb, but I've occasionally been tripped up by multi-line input or output in source-code blocks. The following might solve your problem, if a verbatim insertion of the noweb code is sufficient for you:

#+NAME: vowel_r
#+BEGIN_SRC sh
ar
er
ir
or
ur
#+END_SRC

#+BEGIN_SRC sh :shebang "#!/bin/bash" :noweb yes :results verbatim
printf "Four\nscore\nand\nseven\nyears\nago" | grep -F <<vowel_r>>
#+END_SRC

#+RESULTS:
: years
: score
: Four

I don't know why the output is in reverse order from what you get on the command line!

  • That's a clever solution that I hadn't thought of, but it doesn't work in this case. Unlike vowel_r in my example, the org-babel output I want to use in my actual work is not hard-coded, but is the result of a data munging process. – njc Apr 3 at 20:42
  • I suspected it might be! Another option would be to use the :session flag, which would allow you to share variables between code blocks. – Tyler Apr 3 at 21:11

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.