UP | HOME

Support via Liberapay

sed Code Blocks in Babel

Babel support for sed

sed -- stream-editor

1. Introduction

The Unix utility sed (stream editor) is a non-interactive command-line text editor that can be used to filter text in order to extract part of a file or to substitute multiple occurrences of a string in a file.

Alternatives to sed include awk and Perl.

2. Requirements and Setup

The sed utility was created more than 50 years ago and is currently available on most operating systems.

To enable Babel evaluation of sed code blocks, add a sexp to org-babel-load-languages as follows:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((sed . t))) ;; this line configures Babel =sed= support

3. Babel Features for sed Code Blocks

3.1. Header Arguments

There are no sed-specific default values for header arguments.

There are two sed-specific header arguments. The header argument :cmd-line can be used to pass command line arguments to sed. The header argument :in-file can be used to specify the input file for sed.

3.2. Sessions

The sed utility is non-interactive and does not support sessions.

3.3. Result Types

By default, a sed code block returns a table. This behavior can be changed with header arguments or by passing command line arguments to sed.

4. Examples of Use

The examples all use this input file:

one two three, one two three
four three two one
one hundred

By default, sed code blocks return a table.

#+name: sed_default
#+begin_src sed :in-file ~/temp/sed-test.txt
s/one/ONE/g
#+end_src

#+RESULTS: sed_default
| ONE  | two     | three, | ONE | two | three |
| four | three   | two    | ONE |     |       |
| ONE  | hundred |        |     |     |       |

The header argument :results verbatim returns the contents of the filtered input.

#+name: sed_return
#+begin_src sed :in-file ~/temp/sed-test.txt :results verbatim
s/one/ONE/g
#+end_src

#+RESULTS: sed_return
: ONE two three, ONE two three
: four three two ONE
: ONE hundred

Command line arguments to sed can be passed with the header argument :cmd-line.

#+name: sed_silent
#+begin_src sed :in-file ~/temp/sed-test.txt :results verbatim :cmd-line -n
s/one/ONE/g
#+end_src

#+RESULTS: sed_silent

Here is the obligatory 'Hello, world!' example.

#+name: sed_hello
#+begin_src sed  :in-file ~/temp/sed-test.txt :results verbatim
# convert input text stream to "Hello, world!"
s/.*/Hello, world!/
q
#+end_src

#+RESULTS: sed_hello
: Hello, world!

The output from sed can be directed to a file with the header arguments :file and, optionally :output-dir. The header argument :results file inserts a link to the output file in the Org mode buffer.

Note the comment on the first line of the code block. Note also the last line, which instructs sed to quit.

#+name: sed_file
#+begin_src sed  :in-file ~/temp/sed-test.txt :results file :file sed-results.txt :output-dir ~/temp/
# convert input text stream to "Hello, world!"
s/.*/Hello, world!/
q
#+end_src

#+RESULTS: sed_file
[[file:~/temp/sed-results.txt]]

The file sed-results.txt looks like this:

Hello, world!

5. Links

Preparation of this documentation was aided by Bruce Barnett's introduction and tutorial.

Documentation from the orgmode.org/worg/ website (either in its HTML format or in its Org format) is licensed under the GNU Free Documentation License version 1.3 or later. The code examples and css stylesheets are licensed under the GNU General Public License v3 or later.