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

NAME

6       ed - edit text
7

SYNOPSIS

9       ed [-p string][-s][file]
10

DESCRIPTION

12       The ed utility is a line-oriented text editor that uses two modes: com‐
13       mand mode and input mode. In command mode the input characters shall be
14       interpreted as commands, and in input mode they shall be interpreted as
15       text. See the EXTENDED DESCRIPTION section.
16

OPTIONS

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

OPERANDS

32       The following operand shall be supported:
33
34       file   If the file argument is given, ed shall simulate an e command on
35              the file named by the pathname, file, before accepting  commands
36              from  the  standard  input.  If  the  file  operand is '-' , the
37              results are unspecified.
38
39

STDIN

41       The standard input shall be a text  file  consisting  of  commands,  as
42       described in the EXTENDED DESCRIPTION section.
43

INPUT FILES

45       The input files shall be text files.
46

ENVIRONMENT VARIABLES

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

ASYNCHRONOUS EVENTS

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

STDOUT

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

STDERR

110       The standard error shall be used only for diagnostic messages.
111

OUTPUT FILES

113       The output files shall be text files whose formats are dependent on the
114       editing commands given.
115

EXTENDED DESCRIPTION

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

EXIT STATUS

881       The following exit values shall be returned:
882
883        0     Successful completion without any file or command errors.
884
885       >0     An error occurred.
886
887

CONSEQUENCES OF ERRORS

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

APPLICATION USAGE

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

EXAMPLES

913       None.
914

RATIONALE

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

FUTURE DIRECTIONS

1119       None.
1120

SEE ALSO

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