<s>
sed	B-Application
(	O
"	O
stream	B-Application
editor	I-Application
"	O
)	O
is	O
a	O
Unix	B-Application
utility	I-Application
that	O
parses	O
and	O
transforms	O
text	O
,	O
using	O
a	O
simple	O
,	O
compact	O
programming	O
language	O
.	O
</s>
<s>
sed	B-Application
was	O
based	O
on	O
the	O
scripting	B-Language
features	O
of	O
the	O
interactive	O
editor	O
ed	B-Application
(	O
"	O
editor	O
"	O
,	O
1971	O
)	O
and	O
the	O
earlier	O
qed	B-Application
(	O
"	O
quick	O
editor	O
"	O
,	O
1965	O
–	O
66	O
)	O
.	O
</s>
<s>
It	O
was	O
one	O
of	O
the	O
earliest	O
tools	O
to	O
support	O
regular	B-Language
expressions	I-Language
,	O
and	O
remains	O
in	O
use	O
for	O
text	O
processing	O
,	O
most	O
notably	O
with	O
the	O
substitution	O
command	O
.	O
</s>
<s>
Popular	O
alternative	O
tools	O
for	O
plaintext	O
string	O
manipulation	O
and	O
"	O
stream	O
editing	O
"	O
include	O
AWK	B-Application
and	O
Perl	B-Language
.	O
</s>
<s>
First	O
appearing	O
in	O
Version	B-Operating_System
7	I-Operating_System
Unix	I-Operating_System
,	O
sed	B-Application
is	O
one	O
of	O
the	O
early	O
Unix	B-Application
commands	I-Application
built	O
for	O
command	B-Application
line	I-Application
processing	O
of	O
data	O
files	O
.	O
</s>
<s>
It	O
evolved	O
as	O
the	O
natural	O
successor	O
to	O
the	O
popular	O
grep	B-Application
command	O
.	O
</s>
<s>
The	O
original	O
motivation	O
was	O
an	O
analogue	O
of	O
grep	B-Application
(	O
g/re/p	B-Application
)	O
for	O
substitution	O
,	O
hence	O
"	O
g/re/s	O
"	O
.	O
</s>
<s>
Foreseeing	O
that	O
further	O
special-purpose	O
programs	O
for	O
each	O
command	O
would	O
also	O
arise	O
,	O
such	O
as	O
g/re/d	O
,	O
McMahon	O
wrote	O
a	O
general-purpose	O
line-oriented	O
stream	B-Application
editor	I-Application
,	O
which	O
became	O
sed	B-Application
.	O
</s>
<s>
The	O
syntax	O
for	O
sed	B-Application
,	O
notably	O
the	O
use	O
of	O
/	O
for	O
pattern	B-Language
matching	I-Language
,	O
and	O
s///	O
for	O
substitution	O
,	O
originated	O
with	O
ed	B-Application
,	O
the	O
precursor	O
to	O
sed	B-Application
,	O
which	O
was	O
in	O
common	O
use	O
at	O
the	O
time	O
,	O
and	O
the	O
regular	B-Language
expression	I-Language
syntax	O
has	O
influenced	O
other	O
languages	O
,	O
notably	O
ECMAScript	B-Language
and	O
Perl	B-Language
.	O
</s>
<s>
Later	O
,	O
the	O
more	O
powerful	O
language	O
AWK	B-Application
developed	O
,	O
and	O
these	O
functioned	O
as	O
cousins	O
,	O
allowing	O
powerful	O
text	O
processing	O
to	O
be	O
done	O
by	O
shell	B-Language
scripts	I-Language
.	O
</s>
<s>
sed	B-Application
and	O
AWK	B-Application
are	O
often	O
cited	O
as	O
progenitors	O
and	O
inspiration	O
for	O
Perl	B-Language
,	O
and	O
influenced	O
Perl	B-Language
's	O
syntax	O
and	O
semantics	O
,	O
notably	O
in	O
the	O
matching	O
and	O
substitution	O
operators	O
.	O
</s>
<s>
GNU	B-Operating_System
sed	B-Application
added	O
several	O
new	O
features	O
,	O
including	O
in-place	O
editing	O
of	O
files	O
.	O
</s>
<s>
Super-sed	B-Application
is	O
an	O
extended	O
version	O
of	O
sed	B-Application
that	O
includes	O
regular	B-Language
expressions	I-Language
compatible	O
with	O
Perl	B-Language
.	O
</s>
<s>
Another	O
variant	O
of	O
sed	B-Application
is	O
minised	O
,	O
originally	O
reverse-engineered	O
from	O
4.1BSD	O
sed	B-Application
by	O
Eric	O
S	O
.	O
Raymond	O
and	O
currently	O
maintained	O
by	O
René	O
Rebe	O
.	O
</s>
<s>
minised	O
was	O
used	O
by	O
the	B-Application
GNU	I-Application
Project	I-Application
until	O
the	B-Application
GNU	I-Application
Project	I-Application
wrote	O
a	O
new	O
version	O
of	O
sed	B-Application
based	O
on	O
the	O
new	O
GNU	B-Operating_System
regular	B-Language
expression	I-Language
library	O
.	O
</s>
<s>
The	O
current	O
minised	O
contains	O
some	O
extensions	O
to	O
BSD	O
sed	B-Application
but	O
is	O
not	O
as	O
feature-rich	B-Architecture
as	O
GNU	B-Operating_System
sed	B-Application
.	O
</s>
<s>
It	O
is	O
used	O
on	O
embedded	O
systems	O
and	O
is	O
the	O
version	O
of	O
sed	B-Application
provided	O
with	O
Minix	B-Operating_System
.	O
</s>
<s>
sed	B-Application
is	O
a	O
line-oriented	O
text	O
processing	O
utility	O
:	O
it	O
reads	O
text	O
,	O
line	O
by	O
line	O
,	O
from	O
an	O
input	B-Architecture
stream	I-Architecture
or	O
file	O
,	O
into	O
an	O
internal	O
buffer	O
called	O
the	O
pattern	B-Application
space	I-Application
.	O
</s>
<s>
To	O
the	O
pattern	B-Application
space	I-Application
,	O
sed	B-Application
applies	O
one	O
or	O
more	O
operations	O
which	O
have	O
been	O
specified	O
via	O
a	O
sed	B-Application
script	O
.	O
</s>
<s>
sed	B-Application
implements	O
a	O
programming	O
language	O
with	O
about	O
25	O
commands	O
that	O
specify	O
the	O
operations	O
on	O
the	O
text	O
.	O
</s>
<s>
For	O
each	O
input	O
line	O
,	O
after	O
running	O
the	O
script	O
,	O
sed	B-Application
ordinarily	O
outputs	O
the	O
pattern	B-Application
space	I-Application
(	O
the	O
line	O
as	O
modified	O
by	O
the	O
script	O
)	O
and	O
begins	O
the	O
cycle	O
again	O
with	O
the	O
next	O
line	O
.	O
</s>
<s>
Other	O
end-of-script	O
behaviors	O
are	O
available	O
through	O
sed	B-Application
options	O
and	O
script	O
commands	O
,	O
e.g.	O
</s>
<s>
d	O
to	O
delete	O
the	O
pattern	B-Application
space	I-Application
,	O
q	O
to	O
quit	O
,	O
N	O
to	O
add	O
the	O
next	O
line	O
to	O
the	O
pattern	B-Application
space	I-Application
immediately	O
,	O
and	O
so	O
on	O
.	O
</s>
<s>
Thus	O
a	O
sed	B-Application
script	O
corresponds	O
to	O
the	O
body	O
of	O
a	O
loop	O
that	O
iterates	O
through	O
the	O
lines	O
of	O
a	O
stream	O
,	O
where	O
the	O
loop	O
itself	O
and	O
the	O
loop	O
variable	O
(	O
the	O
current	O
line	O
number	O
)	O
are	O
implicit	O
and	O
maintained	O
by	O
sed	B-Application
.	O
</s>
<s>
The	O
sed	B-Application
script	O
can	O
either	O
be	O
specified	O
on	O
the	O
command	B-Application
line	I-Application
(	O
-e	O
option	O
)	O
or	O
read	O
from	O
a	O
separate	O
file	O
(	O
-f	O
option	O
)	O
.	O
</s>
<s>
Commands	O
in	O
the	O
sed	B-Application
script	O
may	O
take	O
an	O
optional	O
address	O
,	O
in	O
terms	O
of	O
line	O
numbers	O
or	O
regular	B-Language
expressions	I-Language
.	O
</s>
<s>
A	O
separate	O
special	O
buffer	O
,	O
the	O
hold	B-Application
space	I-Application
,	O
may	O
be	O
used	O
by	O
a	O
few	O
sed	B-Application
commands	O
to	O
hold	O
and	O
accumulate	O
text	O
between	O
cycles	O
.	O
</s>
<s>
sed	B-Application
's	O
command	O
language	O
has	O
only	O
two	O
variables	O
(	O
the	O
"	O
hold	B-Application
space	I-Application
"	O
and	O
the	O
"	O
pattern	B-Application
space	I-Application
"	O
)	O
and	O
GOTO-like	O
branching	O
functionality	O
;	O
nevertheless	O
,	O
the	O
language	O
is	O
Turing-complete	O
,	O
and	O
esoteric	O
sed	B-Application
scripts	O
exist	O
for	O
games	O
such	O
as	O
sokoban	B-Device
,	O
arkanoid	B-Device
,	O
chess	B-Application
,	O
and	O
tetris	B-Device
.	O
</s>
<s>
A	O
main	B-General_Concept
loop	I-General_Concept
executes	O
for	O
each	O
line	O
of	O
the	O
input	B-Architecture
stream	I-Architecture
,	O
evaluating	O
the	O
sed	B-Application
script	O
on	O
each	O
line	O
of	O
the	O
input	O
.	O
</s>
<s>
Lines	O
of	O
a	O
sed	B-Application
script	O
are	O
each	O
a	O
pattern-action	O
pair	O
,	O
indicating	O
what	O
pattern	O
to	O
match	O
and	O
which	O
action	O
to	O
perform	O
,	O
which	O
can	O
be	O
recast	O
as	O
a	O
conditional	B-Language
statement	I-Language
.	O
</s>
<s>
Because	O
the	O
main	B-General_Concept
loop	I-General_Concept
,	O
working	O
variables	O
(	O
pattern	B-Application
space	I-Application
and	O
hold	B-Application
space	I-Application
)	O
,	O
input	O
and	O
output	O
streams	O
,	O
and	O
default	O
actions	O
(	O
copy	O
line	O
to	O
pattern	B-Application
space	I-Application
,	O
print	O
pattern	B-Application
space	I-Application
)	O
are	O
implicit	O
,	O
it	O
is	O
possible	O
to	O
write	O
terse	O
one-liner	B-Language
programs	I-Language
.	O
</s>
<s>
For	O
example	O
,	O
the	O
sed	B-Application
program	O
given	O
by	O
:	O
</s>
<s>
The	O
following	O
example	O
shows	O
a	O
typical	O
,	O
and	O
the	O
most	O
common	O
,	O
use	O
of	O
sed	B-Application
:	O
substitution	O
.	O
</s>
<s>
This	O
usage	O
was	O
indeed	O
the	O
original	O
motivation	O
for	O
sed	B-Application
:	O
</s>
<s>
In	O
some	O
versions	O
of	O
sed	B-Application
,	O
the	O
expression	O
must	O
be	O
preceded	O
by	O
-e	O
to	O
indicate	O
that	O
an	O
expression	O
follows	O
.	O
</s>
<s>
The	O
regular	B-Language
expression	I-Language
(	O
i.e.	O
</s>
<s>
Slash	O
(	O
/	O
)	O
is	O
the	O
conventional	O
symbol	O
,	O
originating	O
in	O
the	O
character	O
for	O
"	O
search	O
"	O
in	O
ed	B-Application
,	O
but	O
any	O
other	O
could	O
be	O
used	O
to	O
make	O
syntax	O
more	O
readable	O
if	O
it	O
does	O
not	O
occur	O
in	O
the	O
pattern	O
or	O
replacement	O
;	O
this	O
is	O
useful	O
to	O
avoid	O
"	O
leaning	B-Language
toothpick	I-Language
syndrome	I-Language
"	O
.	O
</s>
<s>
The	O
substitution	O
command	O
,	O
which	O
originates	O
in	O
search-and-replace	O
in	O
ed	B-Application
,	O
implements	O
simple	O
parsing	O
and	O
templating	B-Language
.	O
</s>
<s>
The	O
regexp	B-Language
provides	O
both	O
pattern	B-Language
matching	I-Language
and	O
saving	O
text	O
via	O
sub-expressions	O
,	O
while	O
the	O
replacement	O
can	O
be	O
either	O
literal	O
text	O
,	O
or	O
a	O
format	O
string	O
containing	O
the	O
characters	O
&	O
for	O
"	O
entire	O
match	O
"	O
or	O
the	O
special	O
escape	O
sequences	O
\1	O
through	O
\9	O
for	O
the	O
nth	O
saved	O
sub-expression	O
.	O
</s>
<s>
For	O
example	O
,	O
sed	B-Application
-r	O
"	O
s/	O
( cat|dog	O
)	O
s	O
?	O
/	O
\1s/g	O
 "	O
replaces	O
all	O
occurrences	O
of	O
"	O
cat	O
"	O
or	O
"	O
dog	O
"	O
with	O
"	O
cats	O
"	O
or	O
"	O
dogs	O
"	O
,	O
without	O
duplicating	O
an	O
existing	O
"	O
s	O
"	O
:	O
(	O
cat|dog	O
)	O
is	O
the	O
1st	O
(	O
and	O
only	O
)	O
saved	O
sub-expression	O
in	O
the	O
regexp	B-Language
,	O
and	O
\1	O
in	O
the	O
format	O
string	O
substitutes	O
this	O
into	O
the	O
output	O
.	O
</s>
<s>
Besides	O
substitution	O
,	O
other	O
forms	O
of	O
simple	O
processing	O
are	O
possible	O
,	O
using	O
some	O
25	O
sed	B-Application
commands	O
.	O
</s>
<s>
This	O
example	O
uses	O
some	O
of	O
the	O
following	O
regular	B-Language
expression	I-Language
metacharacters	B-Algorithm
(	O
sed	B-Application
supports	O
the	O
full	O
range	O
of	O
regular	B-Language
expressions	I-Language
)	O
:	O
</s>
<s>
The	O
asterisk	B-Language
( *	O
)	O
matches	O
zero	O
or	O
more	O
occurrences	O
of	O
the	O
previous	O
character	O
.	O
</s>
<s>
Complex	O
sed	B-Application
constructs	O
are	O
possible	O
,	O
allowing	O
it	O
to	O
serve	O
as	O
a	O
simple	O
,	O
but	O
highly	O
specialized	O
,	O
programming	O
language	O
.	O
</s>
<s>
Flow	O
of	O
control	O
,	O
for	O
example	O
,	O
can	O
be	O
managed	O
by	O
the	O
use	O
of	O
a	O
label	O
(	O
a	O
colon	O
followed	O
by	O
a	O
string	O
)	O
and	O
the	O
branch	O
instruction	O
b	O
,	O
as	O
well	O
as	O
the	O
conditional	B-Language
branch	O
t	O
.	O
An	O
instruction	O
b	O
followed	O
by	O
a	O
valid	O
label	O
name	O
will	O
move	O
processing	O
to	O
the	O
command	O
following	O
that	O
label	O
.	O
</s>
<s>
Under	O
Unix	B-Application
,	O
sed	B-Application
is	O
often	O
used	O
as	O
a	O
filter	O
in	O
a	O
pipeline	B-Operating_System
:	O
</s>
<s>
That	O
is	O
,	O
a	O
program	O
such	O
as	O
"	O
generateData	O
"	O
generates	O
data	O
,	O
and	O
then	O
sed	B-Application
makes	O
the	O
small	O
change	O
of	O
replacing	O
x	O
with	O
y	O
.	O
</s>
<s>
It	O
is	O
often	O
useful	O
to	O
put	O
several	O
sed	B-Application
commands	O
,	O
one	O
command	O
per	O
line	O
,	O
into	O
a	O
script	B-Language
file	I-Language
such	O
as	O
subst.sed	O
,	O
and	O
then	O
use	O
the	O
-f	O
option	O
to	O
run	O
the	O
commands	O
(	O
such	O
as	O
s/x/y/g	O
)	O
from	O
the	O
file	O
:	O
</s>
<s>
Any	O
number	O
of	O
commands	O
may	O
be	O
placed	O
into	O
the	O
script	B-Language
file	I-Language
,	O
and	O
using	O
a	O
script	B-Language
file	I-Language
also	O
avoids	O
problems	O
with	O
shell	O
escaping	O
or	O
substitutions	O
.	O
</s>
<s>
Such	O
a	O
script	B-Language
file	I-Language
may	O
be	O
made	O
directly	O
executable	O
from	O
the	O
command	B-Application
line	I-Application
by	O
prepending	O
it	O
with	O
a	O
"	O
shebang	B-Operating_System
line	I-Operating_System
"	O
containing	O
the	O
sed	B-Application
command	O
and	O
assigning	O
the	O
executable	O
permission	O
to	O
the	O
file	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
file	O
subst.sed	O
can	O
be	O
created	O
with	O
contents	O
:	O
</s>
<s>
The	O
file	O
may	O
then	O
be	O
made	O
executable	O
by	O
the	O
current	O
user	O
with	O
the	O
chmod	O
command:The	O
file	O
may	O
then	O
be	O
executed	O
directly	O
from	O
the	O
command	B-Application
line	I-Application
:	O
</s>
<s>
The	O
-i	O
option	O
,	O
introduced	O
in	O
GNU	B-Operating_System
sed	B-Application
,	O
allows	O
in-place	O
editing	O
of	O
files	O
(	O
actually	O
,	O
a	O
temporary	O
output	O
file	O
is	O
created	O
in	O
the	O
background	O
,	O
and	O
then	O
the	O
original	O
file	O
is	O
replaced	O
by	O
the	O
temporary	O
file	O
)	O
.	O
</s>
<s>
script	O
is	O
in	O
a	O
file	O
(	O
e.g.	O
,	O
script.txt	O
)	O
and	O
invoked	O
with	O
sed	B-Application
-f	O
script.txt	O
inputFileName	O
,	O
where	O
"	O
inputFileName	O
"	O
is	O
the	O
input	O
text	O
file	O
.	O
</s>
<s>
and	O
then	O
quits	O
,	O
printing	O
the	O
result	O
before	O
sed	B-Application
exits	O
.	O
</s>
<s>
The	O
example	O
emphasizes	O
many	O
key	O
characteristics	O
of	O
sed	B-Application
:	O
</s>
<s>
Typical	O
sed	B-Application
programs	O
are	O
rather	O
short	O
and	O
simple	O
.	O
</s>
<s>
sed	B-Application
scripts	O
can	O
have	O
comments	O
(	O
the	O
line	O
starting	O
with	O
the	O
#	O
symbol	O
)	O
.	O
</s>
<s>
The	O
s	O
(	O
substitute	O
)	O
command	O
is	O
the	O
most	O
important	O
sed	B-Application
command	O
.	O
</s>
<s>
sed	B-Application
allows	O
simple	O
programming	O
,	O
with	O
commands	O
such	O
as	O
q	O
(	O
quit	O
)	O
.	O
</s>
<s>
sed	B-Application
uses	O
regular	B-Language
expressions	I-Language
,	O
such	O
as	O
.	O
</s>
<s>
Below	O
follow	O
various	O
sed	B-Application
scripts	O
;	O
these	O
can	O
be	O
executed	O
by	O
passing	O
as	O
an	O
argument	O
to	O
sed	B-Application
,	O
or	O
put	O
in	O
a	O
separate	O
file	O
and	O
executed	O
via	O
-f	O
or	O
by	O
making	O
the	O
script	O
itself	O
executable	O
.	O
</s>
<s>
To	O
express	O
the	O
previous	O
example	O
on	O
one	O
line	O
,	O
such	O
as	O
when	O
entering	O
at	O
the	O
command	B-Application
line	I-Application
,	O
one	O
may	O
join	O
two	O
commands	O
via	O
the	O
semicolon	O
:	O
</s>
<s>
In	O
the	O
next	O
example	O
,	O
sed	B-Application
,	O
which	O
usually	O
only	O
works	O
on	O
one	O
line	O
,	O
removes	O
newlines	O
from	O
sentences	O
where	O
the	O
second	O
line	O
starts	O
with	O
one	O
space	O
.	O
</s>
<s>
The	O
sed	B-Application
script	O
below	O
will	O
turn	O
the	O
text	O
above	O
into	O
the	O
following	O
text	O
.	O
</s>
<s>
(	O
N	O
)	O
add	O
the	O
next	O
line	O
to	O
the	O
pattern	B-Application
space	I-Application
;	O
</s>
<s>
(	O
P	O
)	O
print	O
the	O
top	O
line	O
of	O
the	O
pattern	B-Application
space	I-Application
;	O
</s>
<s>
(	O
D	O
)	O
delete	O
the	O
top	O
line	O
from	O
the	O
pattern	B-Application
space	I-Application
and	O
run	O
the	O
script	O
again	O
.	O
</s>
<s>
While	O
simple	O
and	O
limited	O
,	O
sed	B-Application
is	O
sufficiently	O
powerful	O
for	O
a	O
large	O
number	O
of	O
purposes	O
.	O
</s>
<s>
For	O
more	O
sophisticated	O
processing	O
,	O
more	O
powerful	O
languages	O
such	O
as	O
AWK	B-Application
or	O
Perl	B-Language
are	O
used	O
instead	O
.	O
</s>
<s>
These	O
are	O
particularly	O
used	O
if	O
transforming	O
a	O
line	O
in	O
a	O
way	O
more	O
complicated	O
than	O
a	O
regex	B-Language
extracting	O
and	O
template	B-Language
replacement	O
,	O
though	O
arbitrarily	O
complicated	O
transforms	O
are	O
in	O
principle	O
possible	O
by	O
using	O
the	O
hold	O
buffer	O
.	O
</s>
<s>
Conversely	O
,	O
for	O
simpler	O
operations	O
,	O
specialized	O
Unix	B-Application
utilities	I-Application
such	O
as	O
grep	B-Application
(	O
print	O
lines	O
matching	O
a	O
pattern	O
)	O
,	O
head	B-Device
(	O
print	O
the	O
first	O
part	O
of	O
a	O
file	O
)	O
,	O
tail	B-Application
(	O
print	O
the	O
last	O
part	O
of	O
a	O
file	O
)	O
,	O
and	O
tr	B-Application
(	O
translate	O
or	O
delete	O
characters	O
)	O
are	O
often	O
preferable	O
.	O
</s>
<s>
For	O
the	O
specific	O
tasks	O
they	O
are	O
designed	O
to	O
carry	O
out	O
,	O
such	O
specialized	O
utilities	O
are	O
usually	O
simpler	O
,	O
clearer	O
,	O
and	O
faster	O
than	O
a	O
more	O
general	O
solution	O
such	O
as	O
sed	B-Application
.	O
</s>
<s>
The	O
ed/sed	O
commands	O
and	O
syntax	O
continue	O
to	O
be	O
used	O
in	O
descendent	O
programs	O
,	O
such	O
as	O
the	O
text	O
editors	O
vi	B-Application
and	O
vim	B-Application
.	O
</s>
<s>
An	O
analog	O
to	O
ed/sed	O
is	O
sam/ssam	O
,	O
where	O
sam	B-Application
is	O
the	O
Plan	B-Operating_System
9	I-Operating_System
editor	O
,	O
and	O
ssam	O
is	O
a	O
stream	O
interface	O
to	O
it	O
,	O
yielding	O
functionality	O
similar	O
to	O
sed	B-Application
.	O
</s>
