1ED(1P)                     POSIX Programmer's Manual                    ED(1P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       ed - edit text
13

SYNOPSIS

15       ed [-p string][-s][file]
16

DESCRIPTION

18       The ed utility is a line-oriented text editor that uses two modes: com‐
19       mand mode and input mode. In command mode the input characters shall be
20       interpreted as commands, and in input mode they shall be interpreted as
21       text. See the EXTENDED DESCRIPTION section.
22

OPTIONS

24       The  ed  utility  shall  conform  to  the  Base  Definitions  volume of
25       IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
26
27       The following options shall be supported:
28
29       -p  string
30              Use string as  the  prompt  string  when  in  command  mode.  By
31              default, there shall be no prompt string.
32
33       -s     Suppress  the  writing of byte counts by e, E, r, and w commands
34              and of the '!' prompt after a !command.
35
36

OPERANDS

38       The following operand shall be supported:
39
40       file   If the file argument is given, ed shall simulate an e command on
41              the  file named by the pathname, file, before accepting commands
42              from the standard input. If the file operand is '-', the results
43              are unspecified.
44
45

STDIN

47       The  standard  input  shall  be  a text file consisting of commands, as
48       described in the EXTENDED DESCRIPTION section.
49

INPUT FILES

51       The input files shall be text files.
52

ENVIRONMENT VARIABLES

54       The following environment variables shall affect the execution of ed:
55
56       HOME   Determine the pathname of the user's home directory.
57
58       LANG   Provide a default value for the  internationalization  variables
59              that  are  unset  or  null.  (See the Base Definitions volume of
60              IEEE Std 1003.1-2001, Section  8.2,  Internationalization  Vari‐
61              ables  for the precedence of internationalization variables used
62              to determine the values of locale categories.)
63
64       LC_ALL If set to a non-empty string value, override the values  of  all
65              the other internationalization variables.
66
67       LC_COLLATE
68
69              Determine  the  locale  for  the behavior of ranges, equivalence
70              classes, and multi-character collating elements  within  regular
71              expressions.
72
73       LC_CTYPE
74              Determine  the  locale  for  the  interpretation of sequences of
75              bytes of text data as characters (for  example,  single-byte  as
76              opposed  to  multi-byte characters in arguments and input files)
77              and the behavior of character  classes  within  regular  expres‐
78              sions.
79
80       LC_MESSAGES
81              Determine  the  locale  that should be used to affect the format
82              and contents of diagnostic messages written  to  standard  error
83              and informative messages written to standard output.
84
85       NLSPATH
86              Determine the location of message catalogs for the processing of
87              LC_MESSAGES .
88
89

ASYNCHRONOUS EVENTS

91       The ed utility shall take the standard action for all signals (see  the
92       ASYNCHRONOUS  EVENTS section in Utility Description Defaults ) with the
93       following exceptions:
94
95       SIGINT The ed utility shall interrupt its current activity,  write  the
96              string "?\n" to standard output, and return to command mode (see
97              the EXTENDED DESCRIPTION section).
98
99       SIGHUP If the buffer is not empty and has changed since the last write,
100              the  ed utility shall attempt to write a copy of the buffer in a
101              file. First, the file named  ed.hup  in  the  current  directory
102              shall  be  used;  if  that  fails,  the file named ed.hup in the
103              directory named by the HOME environment variable shall be  used.
104              In any case, the ed utility shall exit without returning to com‐
105              mand mode.
106
107       SIGQUIT
108              The ed utility shall ignore this event.
109
110

STDOUT

112       Various editing commands and the prompting feature (see  -p)  write  to
113       standard output, as described in the EXTENDED DESCRIPTION section.
114

STDERR

116       The standard error shall be used only for diagnostic messages.
117

OUTPUT FILES

119       The output files shall be text files whose formats are dependent on the
120       editing commands given.
121

EXTENDED DESCRIPTION

123       The ed utility shall operate on a copy  of  the  file  it  is  editing;
124       changes  made  to  the  copy shall have no effect on the file until a w
125       (write) command is given. The copy of the text is called the buffer.
126
127       Commands to ed have a simple and regular structure: zero, one,  or  two
128       addresses  followed by a single-character command, possibly followed by
129       parameters to that command. These addresses specify one or  more  lines
130       in  the  buffer.  Every  command  that  requires  addresses has default
131       addresses, so that the addresses very often can be omitted. If  the  -p
132       option  is  specified,  the  prompt string shall be written to standard
133       output before each command is read.
134
135       In general, only one command can appear on  a  line.  Certain  commands
136       allow text to be input. This text is placed in the appropriate place in
137       the buffer. While ed is accepting text, it is said to be in input mode.
138       In this mode, no commands shall be recognized; all input is merely col‐
139       lected. Input mode is terminated by entering a line consisting  of  two
140       characters:  a period ( '.' ) followed by a <newline>. This line is not
141       considered part of the input text.
142
143   Regular Expressions in ed
144       The ed utility shall support basic regular expressions, as described in
145       the Base Definitions volume of IEEE Std 1003.1-2001, Section 9.3, Basic
146       Regular Expressions. Since regular expressions in ed are always matched
147       against  single  lines  (excluding  the  terminating <newline>s), never
148       against any larger section of text, there  is  no  way  for  a  regular
149       expression to match a <newline>.
150
151       A null RE shall be equivalent to the last RE encountered.
152
153       Regular expressions are used in addresses to specify lines, and in some
154       commands (for example, the s substitute command) to specify portions of
155       a line to be substituted.
156
157   Addresses in ed
158       Addressing  in  ed  relates to the current line. Generally, the current
159       line is the last line affected by a command. The current line number is
160       the  address  of the current line. If the edit buffer is not empty, the
161       initial value for the current line shall be the last line in  the  edit
162       buffer; otherwise, zero.
163
164       Addresses shall be constructed as follows:
165
166        1. The period character ( '.' ) shall address the current line.
167
168        2. The  dollar  sign  character ( '$' ) shall address the last line of
169           the edit buffer.
170
171        3. The positive decimal number n shall address the  nth  line  of  the
172           edit buffer.
173
174        4. The  apostrophe-x  character  pair  ( "'x" ) shall address the line
175           marked with the mark name character x, which shall be  a  lowercase
176           letter from the portable character set. It shall be an error if the
177           character has not been set to mark a line or if the line  that  was
178           marked is not currently present in the edit buffer.
179
180        5. A  BRE enclosed by slash characters ( '/' ) shall address the first
181           line found by searching forwards from the line following  the  cur‐
182           rent  line  toward  the  end of the edit buffer and stopping at the
183           first line for which the line excluding the  terminating  <newline>
184           matches  the  BRE.  The BRE consisting of a null BRE delimited by a
185           pair of slash characters shall address the next line for which  the
186           line  excluding  the  terminating  <newline>  matches  the last BRE
187           encountered. In addition, the second slash can be  omitted  at  the
188           end  of  a  command  line. Within the BRE, a backslash-slash pair (
189           "\/" ) shall represent a literal slash instead of  the  BRE  delim‐
190           iter.  If  necessary, the search shall wrap around to the beginning
191           of the buffer and continue up to and including the current line, so
192           that the entire buffer is searched.
193
194        6. A  BRE  enclosed  by question-mark characters ( '?' ) shall address
195           the first line found by searching backwards from the line preceding
196           the  current line toward the beginning of the edit buffer and stop‐
197           ping at the first line for which the line excluding the terminating
198           <newline>  matches the BRE. The BRE consisting of a null BRE delim‐
199           ited by a pair of question-mark characters ( "??" )  shall  address
200           the  previous  line  for  which  the line excluding the terminating
201           <newline> matches the last BRE encountered. In addition, the second
202           question-mark  can  be omitted at the end of a command line. Within
203           the BRE, a backslash-question-mark pair ( "\?" ) shall represent  a
204           literal  question  mark instead of the BRE delimiter. If necessary,
205           the search shall wrap around to the end of the buffer and  continue
206           up  to and including the current line, so that the entire buffer is
207           searched.
208
209        7. A plus-sign ( '+' ) or hyphen character ( '-' ) followed by a deci‐
210           mal number shall address the current line plus or minus the number.
211           A plus-sign or hyphen character not followed by  a  decimal  number
212           shall address the current line plus or minus 1.
213
214       Addresses  can  be followed by zero or more address offsets, optionally
215       <blank>-separated. Address offsets are constructed as follows:
216
217        * A plus-sign or hyphen character followed by a decimal  number  shall
218          add  or  subtract, respectively, the indicated number of lines to or
219          from the address. A plus-sign or hyphen character not followed by  a
220          decimal number shall add or subtract 1 to or from the address.
221
222        * A  decimal  number  shall  add  the indicated number of lines to the
223          address.
224
225       It shall not be an error for an intermediate address value to  be  less
226       than zero or greater than the last line in the edit buffer. It shall be
227       an error for the final address value to be less than  zero  or  greater
228       than the last line in the edit buffer. It shall be an error if a search
229       for a BRE fails to find a matching line.
230
231       Commands accept zero, one, or two addresses. If more than the  required
232       number  of  addresses  are  provided  to  a  command that requires zero
233       addresses, it shall be an error. Otherwise, if more than  the  required
234       number  of addresses are provided to a command, the addresses specified
235       first shall be evaluated and then discarded until the maximum number of
236       valid addresses remain, for the specified command.
237
238       Addresses  shall  be  separated  from  each other by a comma ( ',' ) or
239       semicolon character ( ';' ). In the case of a semicolon separator,  the
240       current  line  ( '.' ) shall be set to the first address, and only then
241       will the second address be calculated. This  feature  can  be  used  to
242       determine  the  starting  line for forwards and backwards searches; see
243       rules 5. and 6.
244
245       Addresses can be omitted on either side of the comma or semicolon sepa‐
246       rator, in which case the resulting address pairs shall be as follows:
247
248                               Specified   Resulting
249                               ,           1 , $,
250                               addr        1 , addr
251                               addr ,      addr , addr
252                               ;           . ; $
253                               ; addr      . ; addr
254                               addr ;      addr ; addr
255
256       Any <blank>s included between addresses, address separators, or address
257       offsets shall be ignored.
258
259   Commands in ed
260       In the following list of ed commands, the default addresses  are  shown
261       in  parentheses.  The number of addresses shown in the default shall be
262       the number expected by the command. The parentheses are not part of the
263       address; they show that the given addresses are the default.
264
265       It  is generally invalid for more than one command to appear on a line.
266       However, any command (except e, E, f, q, Q, r, w, and !)  can  be  suf‐
267       fixed by the letter l, n, or p; in which case, except for the l, n, and
268       p commands, the command shall be executed and then the new current line
269       shall  be  written  as  described below under the l, n, and p commands.
270       When an l, n, or p suffix is used with an l, n, or p command, the  com‐
271       mand  shall  write  to  standard  output  as described below, but it is
272       unspecified whether the suffix writes the current  line  again  in  the
273       requested  format or whether the suffix has no effect. For example, the
274       pl command (base p command with an l suffix) shall  either  write  just
275       the  current line or write it twice-once as specified for p and once as
276       specified for l.  Also, the g, G, v, and V commands shall take  a  com‐
277       mand as a parameter.
278
279       Each  address  component  can be preceded by zero or more <blank>s. The
280       command letter can be preceded by zero or more <blank>s.  If  a  suffix
281       letter  (  l,  n,  or p) is given, the application shall ensure that it
282       immediately follows the command.
283
284       The e, E, f, r, and w commands shall take an optional  file  parameter,
285       separated from the command letter by one or more <blank>s.
286
287       If  changes  have been made in the buffer since the last w command that
288       wrote the entire buffer, ed shall warn the user if an attempt  is  made
289       to  destroy  the  editor buffer via the e or q commands. The ed utility
290       shall write the string:
291
292
293              "?\n"
294
295       (followed by an explanatory message if help mode has been  enabled  via
296       the  H  command)  to standard output and shall continue in command mode
297       with the current line number unchanged.  If  the  e  or  q  command  is
298       repeated with no intervening command, it shall take effect.
299
300       If a terminal disconnect is detected:
301
302        * If the buffer is not empty and has changed since the last write, the
303          ed utility shall attempt to write a copy of the  buffer  to  a  file
304          named ed.hup in the current directory. If this write fails, ed shall
305          attempt to write a copy of the buffer to a filename  ed.hup  in  the
306          directory  named  by  the  HOME  environment variable. If both these
307          attempts fail, ed shall exit without saving the buffer.
308
309        * The ed utility shall not write the file to the currently  remembered
310          pathname  or return to command mode, and shall terminate with a non-
311          zero exit status.
312
313       If an end-of-file is detected on standard input:
314
315        * If the ed utility is in input mode, ed shall  terminate  input  mode
316          and  return  to  command  mode.  It  is unspecified if any partially
317          entered lines (that is, input text without a terminating  <newline>)
318          are discarded from the input text.
319
320        * If the ed utility is in command mode, it shall act as if a q command
321          had been entered.
322
323       If the closing delimiter of an RE or of a replacement string (for exam‐
324       ple,  '/'  )  in a g, G, s, v, or V command would be the last character
325       before a <newline>, that delimiter can be omitted, in  which  case  the
326       addressed  line  shall  be written. For example, the following pairs of
327       commands are equivalent:
328
329
330              s/s1/s2   s/s1/s2/p
331              g/s1      g/s1/p
332              ?s1       ?s1?
333
334       If an invalid command is entered, ed shall write the string:
335
336
337              "?\n"
338
339       (followed by an explanatory message if help mode has been  enabled  via
340       the  H  command)  to standard output and shall continue in command mode
341       with the current line number unchanged.
342
343   Append Command
344       Synopsis:
345
346
347              (.)a
348              <text>
349              .
350
351
352       The a command shall read  the  given  text  and  append  it  after  the
353       addressed line; the current line number shall become the address of the
354       last inserted line or, if there were none, the addressed line.  Address
355       0  shall be valid for this command; it shall cause the appended text to
356       be placed at the beginning of the buffer.
357
358   Change Command
359       Synopsis:
360
361
362              (.,.)c
363              <text>
364              .
365
366
367       The c command shall delete the addressed lines, then accept input  text
368       that replaces these lines; the current line shall be set to the address
369       of the last line input; or, if there were none, at the line  after  the
370       last  line  deleted; if the lines deleted were originally at the end of
371       the buffer, the current line number shall be set to the address of  the
372       new  last line; if no lines remain in the buffer, the current line num‐
373       ber shall be set to zero.  Address 0 shall be valid for  this  command;
374       it shall be interpreted as if address 1 were specified.
375
376   Delete Command
377       Synopsis:
378
379
380              (.,.)d
381
382
383       The  d  command  shall delete the addressed lines from the buffer.  The
384       address of the line after the last line deleted shall become  the  cur‐
385       rent  line  number;  if the lines deleted were originally at the end of
386       the buffer, the current line number shall be set to the address of  the
387       new  last line; if no lines remain in the buffer, the current line num‐
388       ber shall be set to zero.
389
390   Edit Command
391       Synopsis:
392
393
394              e [file]
395
396
397       The e command shall delete the entire contents of the buffer  and  then
398       read  in  the file named by the pathname file.  The current line number
399       shall be set to the address of the last line of the buffer. If no path‐
400       name is given, the currently remembered pathname, if any, shall be used
401       (see the f command).  The number of bytes  read  shall  be  written  to
402       standard  output,  unless the -s option was specified, in the following
403       format:
404
405
406              "%d\n", <number of bytes read>
407
408       The name file shall be remembered for possible use as a  default  path‐
409       name in subsequent e, E, r, and w commands. If file is replaced by '!',
410       the rest of the line shall be taken to be a shell  command  line  whose
411       output is to be read. Such a shell command line shall not be remembered
412       as the current file. All marks shall be discarded upon  the  completion
413       of  a  successful  e  command. If the buffer has changed since the last
414       time the entire buffer was  written,  the  user  shall  be  warned,  as
415       described previously.
416
417   Edit Without Checking Command
418       Synopsis:
419
420
421              E [file]
422
423
424       The  E  command  shall possess all properties and restrictions of the e
425       command except that the editor shall  not  check  to  see  whether  any
426       changes have been made to the buffer since the last w command.
427
428   Filename Command
429       Synopsis:
430
431
432              f [file]
433
434
435       If  file  is given, the f command shall change the currently remembered
436       pathname to file; whether the name is changed or  not,  it  shall  then
437       write  the (possibly new) currently remembered pathname to the standard
438       output in the following format:
439
440
441              "%s\n", <pathname>
442
443       The current line number shall be unchanged.
444
445   Global Command
446       Synopsis:
447
448
449              (1,$)g/RE/command list
450
451
452       In the g command, the first step shall be to mark every line for  which
453       the  line  excluding  the  terminating  <newline> matches the given RE.
454       Then, going sequentially from the beginning of the file to the  end  of
455       the  file,  the  given  command  list shall be executed for each marked
456       line, with the current line number set to the address of that line. Any
457       line modified by the command list shall be unmarked. When the g command
458       completes, the current line number shall have the value assigned by the
459       last  command in the command list. If there were no matching lines, the
460       current line number shall not be changed. A single command or the first
461       of  a list of commands shall appear on the same line as the global com‐
462       mand. All lines of a multi-line list except  the  last  line  shall  be
463       ended  with  a backslash preceding the terminating <newline>; the a, i,
464       and c commands and associated input are permitted. The '.'  terminating
465       input  mode  can be omitted if it would be the last line of the command
466       list. An empty command list shall be equivalent to the p  command.  The
467       use  of  the  g,  G,  v, V, and ! commands in the command list produces
468       undefined results. Any character other than <space> or <newline> can be
469       used instead of a slash to delimit the RE. Within the RE, the RE delim‐
470       iter itself can be used as a literal character if it is preceded  by  a
471       backslash.
472
473   Interactive Global Command
474       Synopsis:
475
476
477              (1,$)G/RE/
478
479
480       In  the G command, the first step shall be to mark every line for which
481       the line excluding the terminating  <newline>  matches  the  given  RE.
482       Then, for every such line, that line shall be written, the current line
483       number shall be set to the address of that line, and  any  one  command
484       (other  than one of the a, c, i, g, G, v, and V commands) shall be read
485       and executed. A <newline> shall act  as  a  null  command  (causing  no
486       action to be taken on the current line); an '&' shall cause the re-exe‐
487       cution of the most recent non-null command executed within the  current
488       invocation  of G. Note that the commands input as part of the execution
489       of the G command can address and affect any lines in  the  buffer.  Any
490       line  modified by the command shall be unmarked. The final value of the
491       current line number shall be the value set by the last command success‐
492       fully executed. (Note that the last command successfully executed shall
493       be the G command itself if a command fails or the null command is spec‐
494       ified.)  If there were no matching lines, the current line number shall
495       not be changed. The G command can be terminated by a SIGINT signal. Any
496       character  other  than  <space>  or  <newline> can be used instead of a
497       slash to delimit the RE and the replacement.  Within  the  RE,  the  RE
498       delimiter  itself  can be used as a literal character if it is preceded
499       by a backslash.
500
501   Help Command
502       Synopsis:
503
504
505              h
506
507
508       The h command shall write a  short  message  to  standard  output  that
509       explains  the reason for the most recent '?'  notification. The current
510       line number shall be unchanged.
511
512   Help-Mode Command
513       Synopsis:
514
515
516              H
517
518
519       The H command shall cause ed to enter a mode  in  which  help  messages
520       (see  the h command) shall be written to standard output for all subse‐
521       quent '?' notifications. The H command alternately shall turn this mode
522       on  and  off; it is initially off. If the help-mode is being turned on,
523       the H command also explains the previous '?' notification, if there was
524       one. The current line number shall be unchanged.
525
526   Insert Command
527       Synopsis:
528
529
530              (.)i
531              <text>
532              .
533
534
535       The  i  command  shall insert the given text before the addressed line;
536       the current line is set to the last inserted  line  or,  if  there  was
537       none,  to  the  addressed line. This command differs from the a command
538       only in the placement of the input text. Address 0 shall be  valid  for
539       this command; it shall be interpreted as if address 1 were specified.
540
541   Join Command
542       Synopsis:
543
544
545              (.,.+1)j
546
547
548       The  j  command shall join contiguous lines by removing the appropriate
549       <newline>s. If exactly one address is  given,  this  command  shall  do
550       nothing.  If  lines are joined, the current line number shall be set to
551       the address of the joined line;  otherwise,  the  current  line  number
552       shall be unchanged.
553
554   Mark Command
555       Synopsis:
556
557
558              (.)kx
559
560
561       The  k  command  shall  mark  the addressed line with name x, which the
562       application shall ensure is a lowercase letter from the portable  char‐
563       acter  set. The address "'x" shall then refer to this line; the current
564       line number shall be unchanged.
565
566   List Command
567       Synopsis:
568
569
570              (.,.)l
571
572
573       The l command shall write to standard output the addressed lines  in  a
574       visually  unambiguous  form.  The characters listed in the Base Defini‐
575       tions volume of IEEE Std 1003.1-2001, Table 5-1, Escape  Sequences  and
576       Associated  Actions  ( '\\', '\a', '\b', '\f', '\r', '\t', '\v' ) shall
577       be written as the corresponding escape sequence; the '\n' in that table
578       is  not  applicable. Non-printable characters not in the table shall be
579       written as one three-digit octal number  (with  a  preceding  backslash
580       character)  for  each  byte  in  the  character  (most significant byte
581       first). If the size of a byte on the system is greater than nine  bits,
582       the format used for non-printable characters is implementation-defined.
583
584       Long  lines  shall  be  folded,  with the point of folding indicated by
585       <newline> preceded by a backslash; the length at which  folding  occurs
586       is  unspecified,  but  should be appropriate for the output device. The
587       end of each line shall be marked with a '$', and '$' characters  within
588       the  text shall be written with a preceding backslash. An l command can
589       be appended to any other command other than e, E, f, q, Q, r, w, or  !.
590       The  current  line  number shall be set to the address of the last line
591       written.
592
593   Move Command
594       Synopsis:
595
596
597              (.,.)maddress
598
599
600       The m command shall reposition  the  addressed  lines  after  the  line
601       addressed  by  address.  Address 0 shall be valid for address and cause
602       the addressed lines to be moved to the  beginning  of  the  buffer.  It
603       shall  be  an  error if address address falls within the range of moved
604       lines. The current line number shall be set to the address of the  last
605       line moved.
606
607   Number Command
608       Synopsis:
609
610
611              (.,.)n
612
613
614       The  n command shall write to standard output the addressed lines, pre‐
615       ceding each line by its line number and a <tab>; the current line  num‐
616       ber shall be set to the address of the last line written. The n command
617       can be appended to any command other than e, E, f, q, Q, r, w, or !.
618
619   Print Command
620       Synopsis:
621
622
623              (.,.)p
624
625
626       The p command shall write to standard output the addressed  lines;  the
627       current  line number shall be set to the address of the last line writ‐
628       ten. The p command can be appended to any command other than e,  E,  f,
629       q, Q, r, w, or !.
630
631   Prompt Command
632       Synopsis:
633
634
635              P
636
637
638       The  P  command  shall  cause ed to prompt with an asterisk ( '*' ) (or
639       string, if -p is specified) for all subsequent commands. The P  command
640       alternatively shall turn this mode on and off; it shall be initially on
641       if the -p option is specified; otherwise, off. The current line  number
642       shall be unchanged.
643
644   Quit Command
645       Synopsis:
646
647
648              q
649
650
651       The  q  command shall cause ed to exit. If the buffer has changed since
652       the last time the entire buffer was written, the user shall be  warned,
653       as described previously.
654
655   Quit Without Checking Command
656       Synopsis:
657
658
659              Q
660
661
662       The  Q  command shall cause ed to exit without checking whether changes
663       have been made in the buffer since the last w command.
664
665   Read Command
666       Synopsis:
667
668
669              ($)r [file]
670
671
672       The r command shall read in the file named by  the  pathname  file  and
673       append  it  after the addressed line. If no file argument is given, the
674       currently remembered pathname, if any, shall be used (see the e  and  f
675       commands).   The  currently  remembered  pathname  shall not be changed
676       unless there is no remembered pathname. Address 0 shall be valid for  r
677       and  shall cause the file to be read at the beginning of the buffer. If
678       the read is successful, and -s was not specified, the number  of  bytes
679       read shall be written to standard output in the following format:
680
681
682              "%d\n", <number of bytes read>
683
684       The  current  line  number shall be set to the address of the last line
685       read in. If file is replaced by '!', the rest  of  the  line  shall  be
686       taken  to  be  a  shell command line whose output is to be read. Such a
687       shell command line shall not be remembered as the current pathname.
688
689   Substitute Command
690       Synopsis:
691
692
693              (.,.)s/RE/replacement/flags
694
695
696       The s command shall search each addressed line for an occurrence of the
697       specified  RE  and  replace  either  the  first or all (non-overlapped)
698       matched strings with the replacement; see the following description  of
699       the  g  suffix.  It  is  an  error  if  the substitution fails on every
700       addressed line. Any character other than <space> or  <newline>  can  be
701       used  instead  of a slash to delimit the RE and the replacement. Within
702       the RE, the RE delimiter itself can be used as a literal  character  if
703       it  is  preceded  by  a backslash. The current line shall be set to the
704       address of the last line on which a substitution occurred.
705
706       An ampersand ( '&' ) appearing in the replacement shall be replaced  by
707       the  string matching the RE on the current line. The special meaning of
708       '&' in this context can be suppressed by preceding it by backslash.  As
709       a  more general feature, the characters '\n', where n is a digit, shall
710       be replaced by the text matched  by  the  corresponding  back-reference
711       expression.  When  the  character  '%'  is  the  only  character in the
712       replacement, the replacement used in the most recent substitute command
713       shall  be used as the replacement in the current substitute command; if
714       there was no previous substitute command, the use of '%' in this manner
715       shall be an error. The '%' shall lose its special meaning when it is in
716       a replacement string of more than one character or  is  preceded  by  a
717       backslash.  For each backslash ( '\' ) encountered in scanning replace‐
718       ment from beginning to end, the following character shall lose its spe‐
719       cial  meaning (if any). It is unspecified what special meaning is given
720       to any character other than '&', '\', '%', or digits.
721
722       A line can be split by substituting a <newline> into it.  The  applica‐
723       tion  shall  ensure it escapes the <newline> in the replacement by pre‐
724       ceding it by backslash. Such substitution cannot be done as part of a g
725       or  v command list. The current line number shall be set to the address
726       of the last line on which a substitution is performed. If no  substitu‐
727       tion  is  performed,  the current line number shall be unchanged.  If a
728       line is split, a substitution shall be considered  to  have  been  per‐
729       formed  on each of the new lines for the purpose of determining the new
730       current line number. A substitution shall be considered  to  have  been
731       performed  even  if  the  replacement string is identical to the string
732       that it replaces.
733
734       The application shall ensure that the value of flags is  zero  or  more
735       of:
736
737       count  Substitute  for  the  countth occurrence only of the RE found on
738              each addressed line.
739
740       g      Globally substitute for all non-overlapping instances of the  RE
741              rather  than  just the first one. If both g and count are speci‐
742              fied, the results are unspecified.
743
744       l      Write to standard output the final line in which a  substitution
745              was  made. The line shall be written in the format specified for
746              the l command.
747
748       n      Write to standard output the final line in which a  substitution
749              was  made. The line shall be written in the format specified for
750              the n command.
751
752       p      Write to standard output the final line in which a  substitution
753              was  made. The line shall be written in the format specified for
754              the p command.
755
756
757   Copy Command
758       Synopsis:
759
760
761              (.,.)taddress
762
763
764       The t command shall be equivalent to the m command, except that a  copy
765       of the addressed lines shall be placed after address address (which can
766       be 0); the current line number shall be set to the address of the  last
767       line added.
768
769   Undo Command
770       Synopsis:
771
772
773              u
774
775
776       The  u command shall nullify the effect of the most recent command that
777       modified anything in the buffer, namely the most recent a, c, d, g,  i,
778       j, m, r, s, t, u, v, G, or V command. All changes made to the buffer by
779       a g, G, v, or V global command shall be undone as a single  change;  if
780       no  changes were made by the global command (such as with g/RE/ p), the
781       u command shall have no effect. The current line number shall be set to
782       the value it had immediately before the command being undone started.
783
784   Global Non-Matched Command
785       Synopsis:
786
787
788              (1,$)v/RE/command list
789
790
791       This  command  shall  be equivalent to the global command g except that
792       the lines that are marked during the first  step  shall  be  those  for
793       which  the  line excluding the terminating <newline> does not match the
794       RE.
795
796   Interactive Global Not-Matched Command
797       Synopsis:
798
799
800              (1,$)V/RE/
801
802
803       This command shall be equivalent to the interactive  global  command  G
804       except  that  the  lines that are marked during the first step shall be
805       those for which the line excluding the terminating <newline>  does  not
806       match the RE.
807
808   Write Command
809       Synopsis:
810
811
812              (1,$)w [file]
813
814
815       The  w  command  shall write the addressed lines into the file named by
816       the pathname file. The command shall create the file, if  it  does  not
817       exist,  or  shall  replace  the contents of the existing file. The cur‐
818       rently remembered pathname shall not be  changed  unless  there  is  no
819       remembered  pathname. If no pathname is given, the currently remembered
820       pathname, if any, shall be used (see the e and f commands); the current
821       line  number shall be unchanged. If the command is successful, the num‐
822       ber of bytes written shall be written to standard output, unless the -s
823       option was specified, in the following format:
824
825
826              "%d\n", <number of bytes written>
827
828       If  file  begins  with '!', the rest of the line shall be taken to be a
829       shell command line whose standard input shall be the  addressed  lines.
830       Such  a shell command line shall not be remembered as the current path‐
831       name. This usage of the write command with '!' shall not be  considered
832       as a "last w command that wrote the entire buffer", as described previ‐
833       ously; thus, this alone shall not prevent the warning to the user if an
834       attempt is made to destroy the editor buffer via the e or q commands.
835
836   Line Number Command
837       Synopsis:
838
839
840              ($)=
841
842
843       The line number of the addressed line shall be written to standard out‐
844       put in the following format:
845
846
847              "%d\n", <line number>
848
849       The current line number shall be unchanged by this command.
850
851   Shell Escape Command
852       Synopsis:
853
854
855              !command
856
857
858       The remainder of the line after the '!' shall be sent  to  the  command
859       interpreter  to be interpreted as a shell command line. Within the text
860       of that shell command  line,  the  unescaped  character  '%'  shall  be
861       replaced  with  the  remembered pathname; if a '!' appears as the first
862       character of the command, it shall be replaced with  the  text  of  the
863       previous  shell  command executed via '!' . Thus, "!!" shall repeat the
864       previous !command. If any replacements of '%' or '!' are performed, the
865       modified line shall be written to the standard output before command is
866       executed. The ! command shall write:
867
868
869              "!\n"
870
871       to standard output upon completion, unless the -s option is  specified.
872       The current line number shall be unchanged.
873
874   Null Command
875       Synopsis:
876
877
878              (.+1)
879
880
881       An  address  alone on a line shall cause the addressed line to be writ‐
882       ten.  A <newline> alone shall be equivalent to "+1p" . The current line
883       number shall be set to the address of the written line.
884

EXIT STATUS

886       The following exit values shall be returned:
887
888        0     Successful completion without any file or command errors.
889
890       >0     An error occurred.
891
892

CONSEQUENCES OF ERRORS

894       When  an  error in the input script is encountered, or when an error is
895       detected that is a consequence of the data (not) present in the file or
896       due to an external condition such as a read or write error:
897
898        * If  the standard input is a terminal device file, all input shall be
899          flushed, and a new command read.
900
901        * If the standard input is a regular file, ed shall terminate  with  a
902          non-zero exit status.
903
904       The following sections are informative.
905

APPLICATION USAGE

907       Because  of  the  extremely terse nature of the default error messages,
908       the prudent script writer begins the ed input commands with an  H  com‐
909       mand, so that if any errors do occur at least some clue as to the cause
910       is made available.
911
912       In previous versions, an obsolescent - option was described.   This  is
913       no  longer specified. Applications should use the -s option. Using - as
914       a file operand now produces unspecified results. This allows  implemen‐
915       tations to continue to support the former required behavior.
916

EXAMPLES

918       None.
919

RATIONALE

921       The  initial description of this utility was adapted from the SVID.  It
922       contains some features not found in Version 7 or  BSD-derived  systems.
923       Some of the differences between the POSIX and BSD ed utilities include,
924       but need not be limited to:
925
926        * The BSD - option does not suppress the '!' prompt after a ! command.
927
928        * BSD does not support the special meanings of the '%' and '!'   char‐
929          acters within a ! command.
930
931        * BSD does not support the addresses ';' and ',' .
932
933        * BSD  allows  the  command/suffix  pairs pp, ll, and so on, which are
934          unspecified in this volume of IEEE Std 1003.1-2001.
935
936        * BSD does not support the '!' character part of the e, r, or  w  com‐
937          mands.
938
939        * A  failed  g  command  in  BSD sets the line number to the last line
940          searched if there are no matches.
941
942        * BSD does not default the command list to the p command.
943
944        * BSD does not support the G, h, H, n, or V commands.
945
946        * On BSD, if there is no inserted text, the insert command changes the
947          current line to the referenced line -1; that is, the line before the
948          specified line.
949
950        * On BSD, the join command with only a single address changes the cur‐
951          rent line to that address.
952
953        * BSD  does  not support the P command; moreover, in BSD it is synony‐
954          mous with the p command.
955
956        * BSD does not support the undo of the commands j, m, r, s, or t.
957
958        * The Version 7 ed command W, and the BSD ed commands W, wq, and z are
959          not present in this volume of IEEE Std 1003.1-2001.
960
961       The -s option was added to allow the functionality of the now withdrawn
962       - option in a manner compatible with the Utility Syntax Guidelines.
963
964       In early proposals there was a limit, {ED_FILE_MAX}, that described the
965       historical  limitations of some ed utilities in their handling of large
966       files; some of these have had problems with files  larger  than  100000
967       bytes.  It  was  this  limitation  that  prompted much of the desire to
968       include a split command in this volume of  IEEE Std 1003.1-2001.  Since
969       this  limit  was  removed, this volume of IEEE Std 1003.1-2001 requires
970       that implementations document the file size limits imposed by ed in the
971       conformance  document. The limit {ED_LINE_MAX} was also removed; there‐
972       fore, the global limit {LINE_MAX} is used for input and output lines.
973
974       The manner in which the l command writes non-printable  characters  was
975       changed  to  avoid the historical backspace-overstrike method. On video
976       display terminals, the overstrike is ambiguous because  most  terminals
977       simply  replace  overstruck  characters, making the l format not useful
978       for its intended purpose of unambiguously understanding the content  of
979       the  line.  The  historical backslash escapes were also ambiguous. (The
980       string "a\0011" could represent a line containing those six  characters
981       or  a  line  containing  the three characters 'a', a byte with a binary
982       value of 1, and a 1.) In the format required here, a backslash  appear‐
983       ing  in  the  line is written as "\\" so that the output is truly unam‐
984       biguous. The method of marking the ends of lines was adopted  from  the
985       ex  editor  and is required for any line ending in <space>s; the '$' is
986       placed on all lines so that a real '$' at the end of a line  cannot  be
987       misinterpreted.
988
989       Systems with bytes too large to fit into three octal digits must devise
990       other means of displaying non-printable characters.  Consideration  was
991       given  to  requiring that the number of octal digits be large enough to
992       hold a byte, but this seemed to be too confusing  for  applications  on
993       the  vast majority of systems where three digits are adequate. It would
994       be theoretically possible for the application to use the getconf  util‐
995       ity  to  find  out  the CHAR_BIT value and deal with such an algorithm;
996       however, there is really no portable way that an  application  can  use
997       the  octal  values of the bytes across various coded character sets, so
998       the additional specification was not worthwhile.
999
1000       The description of how a NUL is written was removed. The NUL  character
1001       cannot be in text files, and this volume of IEEE Std 1003.1-2001 should
1002       not dictate behavior in the case of undefined, erroneous input.
1003
1004       Unlike some of the other editing utilities, the filenames  accepted  by
1005       the E, e, R, and r commands are not patterns.
1006
1007       Early  proposals  stated  that  the -p option worked only when standard
1008       input was associated with a terminal device. This has been  changed  to
1009       conform to historical implementations, thereby allowing applications to
1010       interpose themselves between a user and the ed utility.
1011
1012       The form of the substitute command that uses the n suffix  was  limited
1013       in  some historical documentation (where this was described incorrectly
1014       as "backreferencing"). This limit has been omitted because there is  no
1015       reason  why an editor processing lines of {LINE_MAX} length should have
1016       this restriction. The command s/x/X/2047 should be able  to  substitute
1017       the 2047th occurrence of 'x' on a line.
1018
1019       The  use  of  printing commands with printing suffixes (such as pn, lp,
1020       and so on) was made unspecified because BSD-based systems  allow  this,
1021       whereas System V does not.
1022
1023       Some  BSD-based systems exit immediately upon receipt of end-of-file if
1024       all of the lines in the file have been deleted. Since  this  volume  of
1025       IEEE Std 1003.1-2001  refers  to  the  q command in this instance, such
1026       behavior is not allowed.
1027
1028       Some historical implementations returned exit status zero even if  com‐
1029       mand  errors  had  occurred;  this  is  not  allowed  by this volume of
1030       IEEE Std 1003.1-2001.
1031
1032       Some historical implementations contained a bug that allowed  a  single
1033       period  to  be entered in input mode as <backslash> <period> <newline>.
1034       This is not allowed by ed because there is no description  of  escaping
1035       any  of  the characters in input mode; backslashes are entered into the
1036       buffer exactly as typed. The typical method of entering a single period
1037       has  been to precede it with another character and then use the substi‐
1038       tute command to delete that character.
1039
1040       It is difficult under some modes of some versions of historical operat‐
1041       ing  system terminal drivers to distinguish between an end-of-file con‐
1042       dition and terminal disconnect. IEEE Std 1003.1-2001 does  not  require
1043       implementations  to  distinguish between the two situations, which per‐
1044       mits historical implementations of the ed utility on  historical  plat‐
1045       forms  to  conform.   Implementations  are  encouraged  to  distinguish
1046       between the two, if possible, and take appropriate action  on  terminal
1047       disconnect.
1048
1049       Historically,  ed  accepted  a zero address for the a and r commands in
1050       order to insert text at the start of the edit buffer. When  the  buffer
1051       was  empty  the command .= returned zero. IEEE Std 1003.1-2001 requires
1052       conformance to historical practice.
1053
1054       For consistency with the a and r commands and better  user  functional‐
1055       ity,  the  i  and c commands must also accept an address of 0, in which
1056       case 0i is treated as 1i and likewise for the c command.
1057
1058       All of the following are valid addresses:
1059
1060       +++    Three lines after the current line.
1061
1062       /pattern/-
1063              One line before the next occurrence of pattern.
1064
1065       -2     Two lines before the current line.
1066
1067       3 ---- 2
1068              Line one (note the intermediate negative address).
1069
1070       1 2 3  Line six.
1071
1072
1073       Any number of addresses can be provided to commands  taking  addresses;
1074       for  example,  "1,2,3,4,5p"  prints  lines  4 and 5, because two is the
1075       greatest valid number of addresses accepted by the print command. This,
1076       in  combination  with  the semicolon delimiter, permits users to create
1077       commands based on ordered patterns in the file. For example,  the  com‐
1078       mand  "3;/foo/;+2p"  will display the first line after line 3 that con‐
1079       tains the pattern foo, plus the next two lines. Note that  the  address
1080       "3;" must still be evaluated before being discarded, because the search
1081       origin for the "/foo/" command depends on this.
1082
1083       Historically, ed disallowed address chains, as  discussed  above,  con‐
1084       sisting  solely of comma or semicolon separators; for example, ",,," or
1085       ";;;" were considered an error. For consistency of  address  specifica‐
1086       tion,  this  restriction  is removed. The following table lists some of
1087       the address forms now possible:
1088
1089              Address  Addr1  Addr2  Status      Comment
1090              7,       7      7      Historical
1091              7,5,     5      5      Historical
1092              7,5,9    5      9      Historical
1093              7,9      7      9      Historical
1094              7,+      7      8      Historical
1095              ,        1      $      Historical
1096              ,7       1      7      Extension
1097              ,,       $      $      Extension
1098              ,;       $      $      Extension
1099              7;       7      7      Historical
1100              7;5;     5      5      Historical
1101              7;5;9    5      9      Historical
1102              7;5,9    5      9      Historical
1103              7;$;4    $      4      Historical  Valid, but erroneous.
1104              7;9      7      9      Historical
1105              7;+      7      8      Historical
1106              ;        .      $      Historical
1107              ;7       .      7      Extension
1108              ;;       $      $      Extension
1109              ;,       $      $      Extension
1110
1111       Historically, values could be added  to  addresses  by  including  them
1112       after  one  or  more  <blank>s; for example, "3 - 5p" wrote the seventh
1113       line of the file, and "/foo/ 5" was the same as  "5 /foo/"  .  However,
1114       only  absolute  values  could  be  added; for example, "5 /foo/" was an
1115       error. IEEE Std 1003.1-2001 requires conformance  to  historical  prac‐
1116       tice.
1117
1118       Historically,  ed  accepted  the  '^' character as an address, in which
1119       case it was identical to  the  hyphen  character.  IEEE Std 1003.1-2001
1120       does not require or prohibit this behavior.
1121

FUTURE DIRECTIONS

1123       None.
1124

SEE ALSO

1126       Utility Description Defaults, ex, sed, sh, vi
1127
1129       Portions  of  this text are reprinted and reproduced in electronic form
1130       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
1131       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
1132       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
1133       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
1134       event of any discrepancy between this version and the original IEEE and
1135       The  Open Group Standard, the original IEEE and The Open Group Standard
1136       is the referee document. The original Standard can be  obtained  online
1137       at http://www.opengroup.org/unix/online.html .
1138
1139
1140
1141IEEE/The Open Group                  2003                               ED(1P)
Impressum