JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
man pages section 1: User Commands     Oracle Solaris 10 8/11 Information Library
search filter icon
search icon

Document Information

Preface

Introduction

User Commands

acctcom(1)

adb(1)

addbib(1)

admin(1)

aedplot(1B)

alias(1)

allocate(1)

amt(1)

appcert(1)

apptrace(1)

apropos(1)

ar(1)

arch(1)

as(1)

asa(1)

at(1)

atoplot(1B)

atq(1)

atrm(1)

audioconvert(1)

audioplay(1)

audiorecord(1)

auths(1)

auto_ef(1)

awk(1)

banner(1)

basename(1)

basename(1B)

batch(1)

bc(1)

bdiff(1)

bfs(1)

bg(1)

bgplot(1B)

biff(1B)

break(1)

cal(1)

calendar(1)

cancel(1)

case(1)

cat(1)

cc(1B)

cd(1)

cdc(1)

cdrw(1)

chdir(1)

checkeq(1)

checknr(1)

chgrp(1)

chkey(1)

chmod(1)

chown(1)

chown(1B)

ckdate(1)

ckgid(1)

ckint(1)

ckitem(1)

ckkeywd(1)

ckpath(1)

ckrange(1)

ckstr(1)

cksum(1)

cktime(1)

ckuid(1)

ckyorn(1)

clear(1)

cmp(1)

cocheck(1F)

cocreate(1F)

codestroy(1F)

col(1)

comb(1)

comm(1)

command(1)

compress(1)

continue(1)

coproc(1F)

coreceive(1F)

cosend(1F)

cp(1)

cpio(1)

cpp(1)

cputrack(1)

crle(1)

crontab(1)

crtplot(1B)

crypt(1)

csh(1)

csplit(1)

ct(1C)

ctags(1)

ctrun(1)

ctstat(1)

ctwatch(1)

cu(1C)

cut(1)

date(1)

dc(1)

deallocate(1)

decrypt(1)

delta(1)

deroff(1)

df(1B)

dhcpinfo(1)

diff(1)

diff3(1)

diffmk(1)

digest(1)

digestp(1)

dircmp(1)

dirname(1)

dirs(1)

dis(1)

disable(1)

dispgid(1)

dispuid(1)

dos2unix(1)

download(1)

dpost(1)

dtappsession(1)

du(1)

du(1B)

dumbplot(1B)

dump(1)

dumpcs(1)

dumpkeys(1)

echo(1)

echo(1B)

echo(1F)

ed(1)

edit(1)

egrep(1)

eject(1)

elfdump(1)

elfedit(1)

elfsign(1)

elfwrap(1)

enable(1)

encrypt(1)

enhance(1)

env(1)

eqn(1)

errange(1)

errdate(1)

errgid(1)

errint(1)

erritem(1)

error(1)

errpath(1)

errstr(1)

errtime(1)

erruid(1)

erryorn(1)

eval(1)

ex(1)

exec(1)

exit(1)

expand(1)

export(1)

exportfs(1B)

expr(1)

expr(1B)

exstr(1)

face(1)

factor(1)

false(1)

fastboot(1B)

fasthalt(1B)

fc(1)

fdformat(1)

fg(1)

fgrep(1)

file(1)

file(1B)

filep(1)

filesync(1)

filofaxp(1)

find(1)

finger(1)

fmlcut(1F)

fmlexpr(1F)

fmlgrep(1F)

fmli(1)

fmt(1)

fmtmsg(1)

fold(1)

for(1)

foreach(1)

franklinp(1)

from(1B)

ftp(1)

ftpcount(1)

ftpwho(1)

function(1)

gcore(1)

gencat(1)

geniconvtbl(1)

genlayouttbl(1)

genmsg(1)

get(1)

getconf(1)

getfacl(1)

getfrm(1F)

getitems(1F)

getlabel(1)

getopt(1)

getoptcvt(1)

getopts(1)

gettext(1)

gettxt(1)

getzonepath(1)

gigiplot(1B)

glob(1)

goto(1)

gprof(1)

graph(1)

grep(1)

groups(1)

groups(1B)

grpck(1B)

hash(1)

hashcheck(1)

hashmake(1)

hashstat(1)

head(1)

help(1)

helpdate(1)

helpgid(1)

helpint(1)

helpitem(1)

helppath(1)

helprange(1)

helpstr(1)

helptime(1)

helpuid(1)

helpyorn(1)

history(1)

hostid(1)

hostname(1)

hp7221plot(1B)

hpplot(1B)

i286(1)

i386(1)

i486(1)

i860(1)

iAPX286(1)

iconv(1)

idnconv(1)

if(1)

implot(1B)

indicator(1F)

indxbib(1)

install(1B)

ipcrm(1)

ipcs(1)

isainfo(1)

isalist(1)

jobs(1)

join(1)

jsh(1)

kbd(1)

kdestroy(1)

keylogin(1)

keylogout(1)

kill(1)

kinit(1)

klist(1)

kmdb(1)

kmfcfg(1)

kpasswd(1)

krb5-config(1)

ksh(1)

ktutil(1)

lari(1)

last(1)

lastcomm(1)

ld(1)

ld(1B)

ldap(1)

ldapadd(1)

ldapdelete(1)

ldaplist(1)

ldapmodify(1)

ldapmodrdn(1)

ldapsearch(1)

ldd(1)

ld.so.1(1)

let(1)

lex(1)

limit(1)

line(1)

lint(1B)

list_devices(1)

listusers(1)

llc2_autoconfig(1)

llc2_config(1)

llc2_stats(1)

ln(1)

ln(1B)

loadkeys(1)

locale(1)

localedef(1)

logger(1)

logger(1B)

login(1)

logname(1)

logout(1)

longline(1F)

look(1)

lookbib(1)

lorder(1)

lp(1)

lpc(1B)

lpq(1B)

lpr(1B)

lprm(1B)

lpstat(1)

lptest(1B)

ls(1)

ls(1B)

m4(1)

mac(1)

mach(1)

machid(1)

madv.so.1(1)

mail(1)

Mail(1B)

mail(1B)

mailcompat(1)

mailp(1)

mailq(1)

mailstats(1)

mailx(1)

make(1S)

man(1)

mconnect(1)

mcs(1)

mdb(1)

mesg(1)

message(1F)

mixerctl(1)

mkdir(1)

mkmsgs(1)

mkstr(1B)

mktemp(1)

moe(1)

more(1)

mp(1)

mpss.so.1(1)

msgfmt(1)

mt(1)

mv(1)

nawk(1)

nca(1)

ncab2clf(1)

ncakmod(1)

neqn(1)

newform(1)

newgrp(1)

news(1)

newsp(1)

newtask(1)

nice(1)

NIS+(1)

nis+(1)

nis(1)

niscat(1)

nischgrp(1)

nischmod(1)

nischown(1)

nischttl(1)

nisdefaults(1)

niserror(1)

nisgrep(1)

nisgrpadm(1)

nisln(1)

nisls(1)

nismatch(1)

nismkdir(1)

nisopaccess(1)

nispasswd(1)

nisrm(1)

nisrmdir(1)

nistbladm(1)

nistest(1)

nl(1)

nm(1)

nohup(1)

notify(1)

nroff(1)

od(1)

on(1)

onintr(1)

optisa(1)

pack(1)

page(1)

pagesize(1)

pargs(1)

passwd(1)

paste(1)

patch(1)

pathchk(1)

pathconv(1F)

pax(1)

pcat(1)

pcred(1)

pdp11(1)

perl(1)

pfcsh(1)

pfexec(1)

pfiles(1)

pfksh(1)

pflags(1)

pfsh(1)

pg(1)

pgrep(1)

pkginfo(1)

pkgmk(1)

pkgparam(1)

pkgproto(1)

pkgtrans(1)

pkill(1)

pktool(1)

plabel(1)

pldd(1)

plimit(1)

plot(1B)

plottoa(1B)

pmap(1)

popd(1)

postdaisy(1)

postdmd(1)

postio(1)

postmd(1)

postplot(1)

postprint(1)

postreverse(1)

posttek(1)

ppgsz(1)

ppriv(1)

pr(1)

praliases(1)

prctl(1)

preap(1)

prex(1)

print(1)

printenv(1B)

printf(1)

priocntl(1)

proc(1)

prof(1)

profiles(1)

projects(1)

prs(1)

prt(1)

prun(1)

ps(1)

ps(1B)

psig(1)

pstack(1)

pstop(1)

ptime(1)

ptree(1)

pushd(1)

pvs(1)

pwait(1)

pwd(1)

pwdx(1)

ranlib(1)

rcapstat(1)

rcp(1)

rdist(1)

read(1)

readfile(1F)

readonly(1)

red(1)

refer(1)

regcmp(1)

regex(1F)

rehash(1)

reinit(1F)

remote_shell(1)

remsh(1)

renice(1)

repeat(1)

reset(1B)

reset(1F)

return(1)

rksh(1)

rlogin(1)

rm(1)

rmail(1)

rmdel(1)

rmdir(1)

rmformat(1)

roffbib(1)

roles(1)

rpcgen(1)

rpm2cpio(1)

rsh(1)

run(1F)

runat(1)

rup(1)

rup(1C)

ruptime(1)

rusage(1B)

rusers(1)

rwho(1)

sact(1)

sag(1)

sar(1)

sccs(1)

sccs-admin(1)

sccs-cdc(1)

sccs-comb(1)

sccs-delta(1)

sccsdiff(1)

sccs-get(1)

sccs-help(1)

sccs-prs(1)

sccs-prt(1)

sccs-rmdel(1)

sccs-sact(1)

sccs-sccsdiff(1)

sccs-unget(1)

sccs-val(1)

scp(1)

script(1)

sdiff(1)

sed(1)

sed(1B)

select(1)

set(1)

set(1F)

setcolor(1F)

setenv(1)

setfacl(1)

setlabel(1)

setpgrp(1)

settime(1)

sftp(1)

sh(1)

shell(1F)

shell_builtins(1)

shift(1)

shutdown(1B)

size(1)

sleep(1)

snca(1)

snmpstatus(1)

soelim(1)

sort(1)

sortbib(1)

sotruss(1)

source(1)

sparc(1)

spell(1)

spellin(1)

spline(1)

split(1)

srchtxt(1)

ssh(1)

ssh-add(1)

ssh-agent(1)

ssh-http-proxy-connect(1)

ssh-keygen(1)

ssh-keyscan(1)

ssh-socks5-proxy-connect(1)

stop(1)

strchg(1)

strconf(1)

strings(1)

strip(1)

stty(1)

stty(1B)

sum(1)

sum(1B)

sun(1)

suspend(1)

svcprop(1)

svcs(1)

switch(1)

symorder(1)

sysV-make(1)

t300(1)

t300(1B)

t300s(1)

t300s(1B)

t4013(1B)

t4014(1)

t450(1)

t450(1B)

tabs(1)

tail(1)

talk(1)

tar(1)

tbl(1)

tcopy(1)

tee(1)

tek(1)

tek(1B)

telnet(1)

test(1)

test(1B)

test(1F)

tftp(1)

time(1)

timemanp(1)

times(1)

timesysp(1)

timex(1)

tip(1)

tnfdump(1)

tnfxtract(1)

touch(1)

touch(1B)

tplot(1)

tput(1)

tr(1)

tr(1B)

trap(1)

troff(1)

true(1)

truss(1)

tset(1B)

tsort(1)

tty(1)

type(1)

typeset(1)

u370(1)

u3b(1)

u3b15(1)

u3b2(1)

u3b5(1)

ucblinks(1B)

ul(1)

ulimit(1)

umask(1)

unalias(1)

uname(1)

uncompress(1)

unexpand(1)

unget(1)

unhash(1)

unifdef(1)

uniq(1)

units(1)

unix2dos(1)

unlimit(1)

unpack(1)

unset(1)

unset(1F)

unsetenv(1)

until(1)

uptime(1)

users(1B)

uucp(1C)

uudecode(1C)

uuencode(1C)

uuglist(1C)

uulog(1C)

uuname(1C)

uupick(1C)

uustat(1C)

uuto(1C)

uux(1C)

vacation(1)

val(1)

valdate(1)

valgid(1)

valint(1)

valpath(1)

valrange(1)

valstr(1)

valtime(1)

valuid(1)

valyorn(1)

vax(1)

vc(1)

vedit(1)

ver(1)

vgrind(1)

vi(1)

view(1)

vipw(1B)

volcancel(1)

volcheck(1)

volmissing(1)

volrmmount(1)

vplot(1B)

vsig(1F)

w(1)

wait(1)

wc(1)

what(1)

whatis(1)

whence(1)

whereis(1B)

which(1)

while(1)

who(1)

whoami(1B)

whocalls(1)

whois(1)

write(1)

xargs(1)

xgettext(1)

xstr(1)

yacc(1)

yes(1)

ypcat(1)

ypmatch(1)

yppasswd(1)

ypwhich(1)

zcat(1)

zlogin(1)

zonename(1)

ed

, red

- text editor

Synopsis

/usr/bin/ed [-s | -] [-p string] [-x] [-C] [file]
/usr/xpg4/bin/ed [-s | -] [-p string] [-x] [-C] [file]
/usr/xpg6/bin/ed [-s | -] [-p string] [-x] [-C] [file]
/usr/bin/red [-s | -] [-p string] [-x] [-C] [file]

Description

The ed utility is the standard text editor. If file is specified, ed simulates an e command (see below) on the named file. That is, the file is read into ed's buffer so that it can be edited.

The ed utility operates on a copy of the file it is editing. Changes made to the copy have no effect on the file until a w (write) command is given. The copy of the text being edited resides in a temporary file called the buffer. There is only one buffer.

The red utility is a restricted version of ed. It will only allow editing of files in the current directory. red prohibits executing shell commands via !shell command. Attempts to bypass these restrictions result in an error message (restricted shell).

Both ed and red support the fspec(4) formatting capability. The default terminal mode is either stty -tabs or stty tab3, where tab stops are set at eight columns (see stty(1)). If, however, the first line of file contains a format specification, that specification will override the default mode. For example, tab stops would be set at 5, 10, and 15, and a maximum line length of 72 would be imposed if the first line of file contains

<:t5,10,15 s72:>

Commands to ed have a simple and regular structure: zero, one, or two addresses followed by a single-character command, possibly followed by parameters to that command. These addresses specify one or more lines in the buffer. Every command that requires addresses has default addresses, so that the addresses can very often be omitted.

In general, only one command may appear on a line. Certain commands allow the input of text. This text is placed in the appropriate place in the buffer. While ed is accepting text, it is said to be in input mode. In this mode, no commands are recognized; all input is merely collected. Leave input mode by typing a period (.) at the beginning of a line, followed immediately by a carriage return.

/usr/bin/ed

If ed executes commands with arguments, it uses the default shell /usr/bin/sh (see sh(1)).

/usr/xpg4/bin/ed and /usr/xpg6/bin/ed

If ed executes commands with arguments, it uses /usr/xpg4/bin/sh (see ksh(1)).

Regular Expressions

The ed utility supports a limited form of regular expression notation. Regular expressions are used in addresses to specify lines and in some commands (for example, s) to specify portions of a line that are to be substituted. To understand addressing in ed, it is necessary to know that at any time there is a current line. Generally speaking, the current line is the last line affected by a command. The exact effect on the current line is discussed under the description of each command.

Internationalized Basic Regular Expressions are used for all system-supplied locales. See regex(5).

ed Commands

Commands may require zero, one, or two addresses. Commands that require no addresses regard the presence of an address as an error. Commands that accept one or two addresses assume default addresses when an insufficient number of addresses is given; if more addresses are given than such a command requires, the last one(s) are used.

Typically, addresses are separated from each other by a comma (,). They may also be separated by a semicolon (;). In the latter case, the first address is calculated, the current line (.) is set to that value, and then the second address is calculated. This feature can be used to determine the starting line for forward and backward searches (see Rules 5 and 6, above). The second address of any two-address sequence must correspond to a line in the buffer that follows the line corresponding to the first address.

For /usr/xpg6/gbin/ed, the address can be omitted on either side of the comma or semicolon separator, in which case the resulting address pairs are as follows:

Specified
Resulting
,
1 , $
, addr
1 , addr
addr ,
addr , addr
;
1 ; $
; addr
1 ; addr
addr ;
addr ; addr

Any <blank>s included between addresses, address separators, or address offsets are ignored.

In the following list of ed commands, the parentheses shown prior to the command are not part of the address. Rather, the parentheses show the default address(es) for the command.

Each address component can be preceded by zero or more blank characters. The command letter can be preceded by zero or more blank characters. If a suffix letter (l, n, or p) is given, it must immediately follow the command.

The e, E, f, r, and w commands take an optional file parameter, separated from the command letter by one or more blank characters.

If changes have been made in the buffer since the last w command that wrote the entire buffer, ed warns the user if an attempt is made to destroy the editor buffer via the e or q commands. The ed utility writes the string:

"?\n"

(followed by an explanatory message if help mode has been enabled via the H command) to standard output and continues in command mode with the current line number unchanged. If the e or q command is repeated with no intervening command, ed takes effect.

If an end-of-file is detected on standard input when a command is expected, the ed utility acts as if a q command had been entered.

It is generally illegal for more than one command to appear on a line. However, any command (except e, f, r, or w) may be suffixed by l, n, or p in which case the current line is either listed, numbered or written, respectively, as discussed below under the l, n, and p commands.

(.)a
<text>
.

The append command accepts zero or more lines of text and appends it after the addressed line in the buffer. The current line (.) is left at the last inserted line, or, if there were none, at the addressed line. Address 0 is legal for this command: it causes the ``appended'' text to be placed at the beginning of the buffer. The maximum number of characters that may be entered from a terminal is 256 per line (including the new-line character).

(.,.)c
<text>
.

The change command deletes the addressed lines from the buffer, then accepts zero or more lines of text that replaces these lines in the buffer. The current line (.) is left at the last line input, or, if there were none, at the first line that was not deleted. If the lines deleted were originally at the end of the buffer, the current line number will be set to the address of the new last line. If no lines remain in the buffer, the current line number will be set to 0.

/usr/xpg4/bin/ed

Address 0 is not legal for this command.

/usr/xpg6/bin/ed

Address 0 is valid for this command. It is interpreted as if the address 1 were specified.

C

Same as the X command, described later, except that ed assumes all text read in for the e and r commands is encrypted unless a null key is typed in.

(.,.)d

The delete command deletes the addressed lines from the buffer. The line after the last line deleted becomes the current line. If the lines deleted were originally at the end of the buffer, the new last line becomes the current line. If no lines remain in the buffer, the current line number will be set to 0.

e file

The edit command deletes the entire contents of the buffer and then reads the contents of file into the buffer. The current line (.) is set to the last line of the buffer. If file is not given, the currently remembered file name, if any, is used (see the f command). The number of bytes read will be written to standard output, unless the -s option was specified, in the following format:

"%d\n" <number of bytes read>

file is remembered for possible use as a default file name in subsequent e, E, r, and w commands. If file is replaced by !, the rest of the line is taken to be a shell ( sh(1)) command whose output is to be read. Such a shell command is not remembered as the current file name. See also DIAGNOSTICS below. All marks are discarded upon the completion of a successful e command. If the buffer has changed since the last time the entire buffer was written, the user is warned, as described previously.

E file

The Edit command is like e, except that the editor does not check to see if any changes have been made to the buffer since the last w command.

f file

If file is given, the f command changes the currently remembered path name to file. Whether the name is changed or not, the f command then writes the (possibly new) currently remembered path name to the standard output in the following format:

"%s\n"pathname

The current line number is unchanged.

(1,$)g/RE/command list

In the global command, the first step is to mark every line that matches the given RE. Then, for every such line, the given command list is executed with the current line (.) initially set to that line. When the g command completes, the current line number has the value assigned by the last command in the command list. If there were no matching lines, the current line number is not changed. A single command or the first of a list of commands appears on the same line as the global command. All lines of a multi-line list except the last line must be ended with a backslash (\ ); a, i, and c commands and associated input are permitted. The . terminating input mode may be omitted if it would be the last line of the command list. An empty command list is equivalent to the p command. The g, G, v, V, and ! commands are not permitted in the command list. See also the NOTES and the last paragraph before FILES below. Any character other than space or newline can be used instead of a slash to delimit the RE. Within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a backslash.

(1,$)G/RE/

In the interactive Global command, the first step is to mark every line that matches the given RE. Then, for every such line, that line is written to standard output, the current line (.) is changed to that line, and any one command (other than one of the a, c, i, g, G, v, and V commands) may be input and is executed. After the execution of that command, the next marked line is written, and so on. A new-line acts as a null command. An & causes the re-execution of the most recent non-null command executed within the current invocation of G. Note: The commands input as part of the execution of the G command may address and affect any lines in the buffer. The final value of the current line number is the value set by the last command successfully executed. (Notice that the last command successfully executed is the G command itself if a command fails or the null command is specified.) If there were no matching lines, the current line number is not changed. The G command can be terminated by a SIGINT signal. The G command can be terminated by an interrupt signal (ASCII DEL or BREAK). Any character other than space or newline can be used instead of a slash to delimit the RE. Within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a backslash.

h

The help command gives a short error message that explains the reason for the most recent ? diagnostic. The current line number is unchanged.

H

The Help command causes ed to enter a mode in which error messages are written for all subsequent ? diagnostics. It also explains the previous ? if there was one. The H command alternately turns this mode on and off; it is initially off. The current line number is unchanged.

(.,.)i
<text>
.

The insert command accepts zero or more lines of text and inserts it before the addressed line in the buffer. The current line (.) is left at the last inserted line, or, if there were none, at the addressed line. This command differs from the a command only in the placement of the input text. The maximum number of characters that may be entered from a terminal is 256 per line (including the new-line character).

/usr/xpg4/bin/ed

Address 0 is not legal for this command.

/usr/xpg6/bin/ed

Address 0 is valid for this command. It is interpreted as if the address 1 were specified.

(.,.+1)j

The join command joins contiguous lines by removing the appropriate new-line characters. If exactly one address is given, this command does nothing. If lines are joined, the current line number is set to the address of the joined line. Otherwise, the current line number is unchanged.

(.)kx

The mark command marks the addressed line with name x, which must be an ASCII lower-case letter (a-z). The address ´x then addresses this line. The current line (.) is unchanged.

(.,.)l

The l command writes to standard output the addressed lines in a visually unambiguous form. The characters ( \\, \a, \b, \f, \r, \t, \v) are written as the corresponding escape sequence. The \n in that table is not applicable. Non-printable characters not in the table are written as one three-digit octal number (with a preceding backslash character) for each byte in the character, with the most significant byte first.

Long lines are folded, with the point of folding indicated by writing backslash/newline character. The length at which folding occurs is unspecified, but should be appropriate for the output device. The end of each line is marked with a $. When using the /usr/xpg6/bin/ed command, the end of each line is marked with a $ due to folding, and $ characters within the text are written with a preceding backslash. An l command can be appended to any other command other than e, E, f, q, Q, r, w, or !. The current line number is set to the address of the last line written.

(.,.)ma

The move command repositions the addressed line(s) after the line addressed by a. Address 0 is legal for a and causes the addressed line(s) to be moved to the beginning of the file. It is an error if address a falls within the range of moved lines. The current line (.) is left at the last line moved.

(.,.)n

The number command writes the addressed lines, preceding each line by its line number and a tab character. The current line (.) is left at the last line written. The n command may be appended to any command other than e, E, f, q, Q, r, w, or !.

(.,.)p

The print command writes the addressed lines to standard output. The current line (.) is left at the last line written. The p command may be appended to any command other than e, E, f, q, Q, r, w, or !. For example, dp deletes the current line and writes the new current line.

P

The P command causes ed to prompt with an asterisk (*) (or string, if -p is specified) for all subsequent commands. The P command alternatively turns this mode on and off; it is initially on if the -p option is specified, otherwise off. The current line is unchanged.

q

The quit command causes ed to exit. If the buffer has changed since the last time the entire buffer was written, the user is warned. See DIAGNOSTICS.

Q

The editor exits without checking if changes have been made in the buffer since the last w command.

($)r file

The read command reads the contents of file into the buffer. If file is not given, the currently remembered file name, if any, is used (see the e and f commands). The currently remembered file name is not changed unless file is the very first file name mentioned since ed was invoked. Address 0 is legal for r and causes the file to be read in at the beginning of the buffer. If the read is successful and the -s option was not specified, the number of characters read is written to standard output in the following format:

%d\n, <number of bytes read>

The current line (.) is set to the last line read. If file is replaced by !, the rest of the line is taken to be a shell command (see sh(1)) whose output is to be read. For example, $r !ls appends the current directory to the end of the file being edited. Such a shell command is not remembered as the current file name.

(.,.)s/RE/replacement/
(.,.)s/RE/replacement/count, count=[1-2047]
(.,.)s/RE/replacement/g
(.,.)s/RE/replacement/l
(.,.)s/RE/replacement/n
(.,.)s/RE/replacement/p

The substitute command searches each addressed line for an occurrence of the specified RE. Zero or more substitution commands can be specified. In each line in which a match is found, all (non-overlapped) matched strings are replaced by the replacement if the global replacement indicator g appears after the command. If the global indicator does not appear, only the first occurrence of the matched string is replaced. If a number count appears after the command, only the count-th occurrence of the matched string on each addressed line is replaced. It is an error if the substitution fails on all addressed lines. Any character other than space or new-line may be used instead of the slash (/) to delimit the RE and the replacement. The current line (.) is left at the last line on which a substitution occurred. Within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a backslash. See also the last paragraph before FILES below.

An ampersand (&) appearing in the replacement is replaced by the string matching the RE on the current line. The special meaning of & in this context may be suppressed by preceding it by \ . As a more general feature, the characters \n, where n is a digit, are replaced by the text matched by the n-th regular subexpression of the specified RE enclosed between \( and \). When nested parenthesized subexpressions are present, n is determined by counting occurrences of \( starting from the left. When the character % is the only character in the replacement, the replacement used in the most recent substitute command is used as the replacement in the current substitute command. If there was no previous substitute command, the use of % in this manner is an error. The % loses its special meaning when it is in a replacement string of more than one character or is preceded by a \ . For each backslash (\) encountered in scanning replacement from beginning to end, the following character loses its special meaning (if any). It is unspecified what special meaning is given to any character other than &, \, %, or digits.

A line may be split by substituting a new-line character into it. The new-line in the replacement must be escaped by preceding it by \. Such substitution cannot be done as part of a g or v command list. The current line number is set to the address of the last line on which a substitution is performed. If no substitution is performed, the current line number is unchanged. If a line is split, a substitution is considered to have been performed on each of the new lines for the purpose of determining the new current line number. A substitution is considered to have been performed even if the replacement string is identical to the string that it replaces.

The substitute command supports the following indicators:

count

Substitute for the countth occurrence only of the RE found on each addressed line. count must be between 1-2047.

g

Globally substitute for all non-overlapping instances of the RE rather than just the first one. If both g and count are specified, the results are unspecified.

l

Write to standard output the final line in which a substitution was made. The line is written in the format specified for the l command.

n

Write to standard output the final line in which a substitution was made. The line is written in the format specified for the n command.

p

Write to standard output the final line in which a substitution was made. The line will be written in the format specified for the p command.

(.,.)ta

This command acts just like the m command, except that a copy of the addressed lines is placed after address a (which may be 0). The current line (.) is left at the last line copied.

u

The undo command nullifies the effect of the most recent command that modified anything in the buffer, namely the most recent a, c, d, g, i, j, m, r, s, t, u, v, G, or V command. All changes made to the buffer by a g, G, v, or V global command is undone as a single change.If no changes were made by the global command (such as with g/ RE/p), the u command has no effect. The current line number is set to the value it had immediately before the command being undone started.

(1,$)v/RE/command list

This command is the same as the global command g, except that the lines marked during the first step are those that do not match the RE.

(1,$)V/RE/

This command is the same as the interactive global command G, except that the lines that are marked during the first step are those that do not match the RE.

(1,$)w file

The write command writes the addressed lines into file. If file does not exist, it is created with mode 666 (readable and writable by everyone), unless your file creation mask dictates otherwise. See the description of the umask special command on sh(1). The currently remembered file name is not changed unless file is the very first file name mentioned since ed was invoked. If no file name is given, the currently remembered file name, if any, is used (see the e and f commands). The current line (.) is unchanged. If the command is successful, the number of characters written is printed, unless the -s option is specified in the following format:

"%d\n",<number of bytes written>

If file is replaced by !, the rest of the line is taken to be a shell (see sh(1)) command whose standard input is the addressed lines. Such a shell command is not remembered as the current path name. This usage of the write command with ! is to be considered as a ``last w command that wrote the entire buffer''.

(1,$)W file

This command is the same as the write command above, except that it appends the addressed lines to the end of file if it exists. If file does not exist, it is created as described above for the w command.

X

An educated guess is made to determine whether text read for the e and r commands is encrypted. A null key turns off encryption. Subsequent e, r, and w commands will use this key to encrypt or decrypt the text. An explicitly empty key turns off encryption. Also, see the -x option of ed.

($)=

The line number of the addressed line is written to standard output in the following format:

"%d\n"<line number>

The current line number is unchanged by this command.

!shell command

The remainder of the line after the ! is sent to the UNIX system shell (see sh(1)) to be interpreted as a command. Within the text of that command, the unescaped character % is replaced with the remembered file name. If a ! appears as the first character of the shell command, it is replaced with the text of the previous shell command. Thus, !! repeats the last shell command. If any replacements of % or ! are performed, the modified line is written to the standard output before command is executed. The ! command will write:

"!\n"

to standard output upon completion, unless the -s option is specified. The current line number is unchanged.

(.+1)<new-line>

An address alone on a line causes the addressed line to be written. A new-line alone is equivalent to .+1p. It is useful for stepping forward through the buffer. The current line number will be set to the address of the written line.

If an interrupt signal (ASCII DEL or BREAK) is sent, ed writes a "?\n" and returns to its command level.

The ed utility takes the standard action for all signals with the following exceptions:

SIGINT

The ed utility interrupts its current activity, writes the string "?\n" to standard output, and returns to command mode.

SIGHUP

If the buffer is not empty and has changed since the last write, the ed utility attempts to write a copy of the buffer in a file. First, the file named ed.hup in the current directory is used. If that fails, the file named ed.hup in the directory named by the HOME environment variable is used. In any case, the ed utility exits without returning to command mode.

Some size limitations are in effect: 512 characters in a line, 256 characters in a global command list, and 255 characters in the path name of a file (counting slashes). The limit on the number of lines depends on the amount of user memory. Each line takes 1 word.

When reading a file, ed discards ASCII and NUL characters.

If a file is not terminated by a new-line character, ed adds one and puts out a message explaining what it did.

If the closing delimiter of an RE or of a replacement string (for example, /) would be the last character before a new-line, that delimiter may be omitted, in which case the addressed line is written. The following pairs of commands are equivalent:

s/s1/s2

s/s1/s2/p

g/s1

g/s1/p

?s1

?s1?

If an invalid command is entered, ed writes the string:

"?\n"

(followed by an explanatory message if help mode has been enabled by the H command) to standard output and continues in command mode with the current line number unchanged.

Options

-C

Encryption option. The same as the -x option, except that ed simulates a C command. The C command is like the X command, except that all text read in is assumed to have been encrypted.

-pstring

Allows the user to specify a prompt string. By default, there is no prompt string.

-s | -;

Suppresses the writing of character counts by e, r, and w commands, of diagnostics from e and q commands, and of the ! prompt after a !shell command.

-x

Encryption option. When -x is used, ed simulates an X command and prompts the user for a key. The X command makes an educated guess to determine whether text read in is encrypted or not. The temporary buffer file is encrypted also, using a transformed version of the key typed in for the -x option. See NOTES.

Operands

The following operand is supported:

file

If file is specified, ed simulates an e command on the file named by the path name file before accepting commands from the standard input.

Usage

See largefile(5) for the description of the behavior of ed and red when encountering files greater than or equal to 2 Gbyte ( 231 bytes).

Environment Variables

See environ(5) for descriptions of the following environment variables that affect the execution of ed: HOME, LANG, LC_ALL, LC_CTYPE, LC_COLLATE, LC_MESSAGES, and NLSPATH.

Exit Status

The following exit values are returned:

0

Successful completion without any file or command errors.

>0

An error occurred.

Files

$TMPDIR

If this environment variable is not NULL, its value is used in place of /var/tmp as the directory name for the temporary work file.

/var/tmp

If /var/tmp exists, it is used as the directory name for the temporary work file.

/tmp

If the environment variable TMPDIR does not exist or is NULL, and if /var/tmp does not exist, then /tmp is used as the directory name for the temporary work file.

ed.hup

Work is saved here if the terminal is hung up.

Attributes

See attributes(5) for descriptions of the following attributes:

/usr/bin/ed, /usr/bin/red

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Availability
SUNWcsu
CSI
Enabled

/usr/xpg4/bin/ed

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Availability
SUNWxcu4
CSI
Enabled
Interface Stability
Standard

/usr/xpg6/bin/ed

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Availability
SUNWxcu6
CSI
Enabled
Interface Stability
Standard

See Also

bfs(1), edit(1), ex(1), grep(1), ksh(1), sed(1), sh(1), stty(1), umask(1), vi(1), fspec(4), attributes(5), environ(5), largefile(5), regex(5), standards(5)

Diagnostics

?

for command errors.

?file

for an inaccessible file. Use the help and Help commands for detailed explanations.

If changes have been made in the buffer since the last w command that wrote the entire buffer, ed warns the user if an attempt is made to destroy ed's buffer via the e or q commands. It writes ? and allows one to continue editing. A second e or q command at this point will take effect. The -s command-line option inhibits this feature.

Notes

The - option, although it continues to be supported, has been replaced in the documentation by the -s option that follows the Command Syntax Standard (see Intro(1)).

A ! command cannot be subject to a g or a v command.

The ! command and the ! escape from the e, r, and w commands cannot be used if the editor is invoked from a restricted shell (see sh(1)).

The sequence \n in an RE does not match a new-line character.

If the editor input is coming from a command file (for example, ed file < ed_cmd_file), the editor exits at the first failure.

Loading an alternate malloc() library using the environment variable LD_PRELOAD can cause problems for /usr/bin/ed.