esolang

sed

The Unix stream editor. Regular expressions, a second buffer and a goto turn out to be a whole computer.

Lee E. McMahon, 1974 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 2 known programs · Test method

Run it

Expected: Hello, World!

The idea

Lee E. McMahon developed sed, and it came out in 1974 as one of the first tools to support regular expressions. It reads its input one line at a time into the pattern space, runs the script over it and prints what is left. A second buffer, the hold space, keeps text from one line to the next. Substitution, these two buffers and jumps to labels are enough for any computation; in 2001 Christophe Blaess ran a Turing machine in sed. The specification lists programs written in it, from dc to sokoban, chess and tetris.

Read the story

This implementation runs the commands of the specification's table the way GNU sed does.

The machine

CommandDoes
s/re/text/Replace what the regex matches; & is the match and \1 a group; flags g p N I M
y/abc/xyz/Swap characters one for one
p PPrint the pattern space, or its first line
d DDelete it and start the next cycle, or delete its first line and run the script again
n NReplace it with the next line, or add the next line to it
h H g G xCopy, add or swap between the pattern and hold spaces
a i cPrint text after, before or instead of the line
: b t TA label, a jump, a jump when a substitution has or has not worked
= q QPrint the line number; quit with or without printing
{ }A block of commands under one address

An address picks the lines a command works on. It is a line number, $ for the last line, a regex /re/, first~step or a range such as 2,5 or /a/,/b/, and ! after it turns it around. Regular expressions are GNU basic ones, so a group is written \( and \). There are no command-line options but a first line of #n does what -n does, as POSIX says.

Example

The Hello World replaces the first input line and quits, which still prints it.

The Hello World replaces the first input line and quits, which still prints it.

The line count prints the line number on the last line, $, and deletes every line so that nothing else is printed.

Expected: 3

A well known one-liner reverses a line. It adds a line feed, then moves characters one at a time from the front to just after the line feed. D deletes up to the line feed and runs the script again on what is left.

Expected: desserts

The triangle needs the options -r and -n, which this implementation does not take. Written with basic groups and #n it runs. Each pass prints the first line and moves one character up into it.

Expected: a aa aaa aaaa

Patterns it teaches

Further

Also in Rewriting

Write your own in Run Convert a program from sed

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: sed.

Command line

shib run sed -e '$=;d' --input 'a
b
c
'
shib manual sed

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/sed.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org