1#! /bin/sed -nf
2
3# $OpenBSD: sierpinski.sed,v 1.1 2008/10/10 14:33:34 millert Exp $
4# From http://sed.sourceforge.net/grabbag/scripts
5# Public Domain
6
7# Sierpinski triangle in 10 commands + 2 labels.
8# Start with a line like this
9# _______________________________X_____________________________
10
11# Put an equal number of underscores on both sides.
12s/^\(_*\).*/\1X\1/p
13
14# Construct the last three lines of the triangle
15:start
16/^X/!s/_X_/X_X/gp
17/^X/!s/_X_X_/X___X/gp
18/^X/!s/_X___X_/X_X_X_X/gp
19/^X/ d
20
21# Now replace the consecutive X's with an X and many colons
22# X_X_X_X --->
23# X::::::
24:loop
25s/\(X:*\)_X/\1::/g
26tloop
27
28# And now create two new "seeds", one to the left and one to the right
29# _X::::::_ --->
30# X:::::::X --->
31# X_______X
32
33s/_X\(::*\)_/X:\1X/g
34s/:/_/gp
35bstart
36