Hi All:
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.
Best,
Steve
On 4/6/26 12:43 PM, Steve Holtz wrote:
Hi All:
I've got a couple of source code blocks that I use while teaching CS
courses.
Here's a patch for one of them: It uses sed to extract selected lines
from the output generated by one source code block (that does not get
exported) to generate a second source code block (that is exported).
The others will follow when I have time....
Best regards,
Steve
--
Steven J Holtz
Instructor
Department of Computer Science
University of Minnesota Duluth
https://www.d.umn.edu/~sholtz/
--
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