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/
From a5c308380878cb1b730e27417986d7f9e908f974 Mon Sep 17 00:00:00 2001
From: Steven J Holtz <[email protected]>
Date: Mon, 6 Apr 2026 12:36:58 -0500
Subject: [PATCH] Extract only selected lines from output from source code
 block.

---
 library-of-babel.org | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/library-of-babel.org b/library-of-babel.org
index 0e35313c..6bc17df9 100644
--- a/library-of-babel.org
+++ b/library-of-babel.org
@@ -611,3 +611,39 @@ 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

Reply via email to