Hi Christian:
Thanks for the feedback and guidance.
Attached please find an updated patch.
Please provide any further guidance you might have.
Best Regards,
Steve
On 4/12/26 4:07 PM, Christian Moe wrote:
Ihor Radchenko<[email protected]> writes:
Steve Holtz via "General discussions about Org-mode."
<[email protected]> writes:
After sending I realized that there was an error in the code... Hyphens
needed replaced by underbars. My bad.... I made the change without
testing....
The attached version has the fix - and has been tested.
Thanks for this, Steve!
A couple of thoughts and questions before we add it to the library:
* Extract lines from output
Perhaps add "with sed".
You have added the new section at the end of the page, but I think it
can come earlier. The last couple of entries before it are not actual
LoB blocks but relevant Babel tips ("Available languages", "Default
result"). Since your block deals with passing values, perhaps it belongs
right before or right after the "Tables" section.
To use this, name the source code block that will be generating the
lines to extract from. Usually, this source code block will not export
anything. So, it will look like this:
#+name: source
#+header: :results output :exports none
#+begin_src ???
???
#+end_src
As a non-working example, it should probably be in a BEGIN_EXAMPLE block
with `#+' lines comma-escaped, so it doesn't get ingested into the
Library of Babel.
But I'm just wondering: instead of the question marks, can we think of a
working minimal source block that does something it might make sense to
extract lines from?
And the =line_str= variable must be passed the lines desired to
extract using the =sed= syntax for
[[https://www.gnu.org/software/sed/manual/sed.html#Range-Addresses][selecting
by address ranges]]
([[https://www.gnu.org/software/sed/manual/sed.html#Numeric-Addresses][selecting
by line numbers]] or by
[[https://www.gnu.org/software/sed/manual/sed.html#Regexp-Addresses][text
matching]] may work, but has not been tested by the author).
Is this the right way to put it? As I understand the sed manual,
'addresses' can be line numbers or regular expressions, and 'address
ranges' can be composed of both. The address ranges in your example CALL
line are composed of line-number pairs (and one single line number,
`7p'), so it seems fair to say that line numbers *have* been tested.
FWIW, I've tested text matching a bit and that seems to work fine, too.
#+call: extract_lines(data=source(), line-str="1,2p;7p;14,16p") [...]
^
line_str
Regards,
Christian
--
Steven J Holtz
Instructor
Department of Computer Science
University of Minnesota Duluth
https://www.d.umn.edu/~sholtz/
From c936a424f40a06e83ed06e20cc43d0d6331d716e Mon Sep 17 00:00:00 2001
From: Steven J Holtz <[email protected]>
Date: Mon, 6 Apr 2026 15:09:08 -0500
Subject: [PATCH] Extract only selected lines from output from source code
block.
---
library-of-babel.org | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/library-of-babel.org b/library-of-babel.org
index 0e35313c..2fed70e9 100644
--- a/library-of-babel.org
+++ b/library-of-babel.org
@@ -611,3 +611,40 @@ default behavior. Below is a list of such options:
| Org Babel file | Default result | Option |
|----------------+----------------+--------------------------------------------|
| ob-shell.el | output | org-babel-shell-results-defaults-to-output |
+
+* Extract lines from output
+
+To extract only certain lines from the output of a source code block.
+
+#+name: extract_lines
+#+header: :var data="" line_str=""
+#+header: :results output
+#+begin_src sh
+ echo "${data}" | sed -n "${line_str}"
+#+end_src
+
+To use this, name the source code block that will be generating the
+lines to extract from. Usually, this source code block will not export
+anything. So, it will look like this:
+
+#+name: source
+#+header: :results output :exports none
+#+begin_src ???
+ ???
+#+end_src
+
+The =data= variable in =extract_lines= must be set to the output from
+this source code block.
+
+And the =line_str= variable must be passed the lines desired to
+extract using the =sed= syntax for [[https://www.gnu.org/software/sed/manual/sed.html#Range-Addresses][selecting by address ranges]]
+([[https://www.gnu.org/software/sed/manual/sed.html#Numeric-Addresses][selecting by line numbers]] or by [[https://www.gnu.org/software/sed/manual/sed.html#Regexp-Addresses][text matching]] may work, but has not
+been tested by the author). Multiple ranges can be selected by
+separating them by semicolons.
+
+Then call the =extract_lines= block with, for example:
+
+#+call: extract_lines(data=source(), line-str="1,2p;7p;14,16p") :results output :exports results
+
+to extract lines 1–2, 7, and 14–16 from the output generated by the
+=source= source code block.
--
2.43.0