BSD 3 development
[unix-history] / usr / doc / sed
CommitLineData
2074ceed
BJ
1.hw de-limit
2.hw de-limit-ing
3.RP
4....TM 78-1270-1 39199 39199-11
5.ND August 15, 1978
6.TL
7SED \(em A Non-interactive Text Editor
8.AU "MH 2C-555" 3302
9Lee E. McMahon
10.AI
11.MH
12.OK
13Context search
14Editing
15.AB
16.ul
17Sed
18is a non-interactive context editor
19that runs on the
20.UX
21operating system.
22.ul
23Sed
24is
25designed to be especially useful in
26three cases:
27.in +1i
28.LP
29.ti -.5i
301)
31To edit files too large for comfortable
32interactive editing;
33.ti -.5i
342)
35To edit any size file when the sequence
36of editing commands is too complicated to be comfortably
37typed in interactive mode.
38.ti -.5i
393)
40To perform multiple `global' editing functions
41efficiently in one pass through the input.
42.in -1i
43.LP
44This memorandum constitutes a manual for users of
45.ul
46sed.
47.AE
48.CS 10 0 10 0 0 1
49.SH
50Introduction
51.LP
52.ul
53Sed
54is a non-interactive context editor designed to be especially useful in
55three cases:
56.in +1i
57.LP
58.ti -.5i
591)
60To edit files too large for comfortable
61interactive editing;
62.ti -.5i
632)
64To edit any size file when the sequence
65of editing commands is too complicated to be comfortably
66typed in interactive mode;
67.ti -.5i
683)
69To perform multiple `global' editing functions
70efficiently in one pass through the input.
71.in -1i
72.LP
73Since only a few lines of the input reside in core
74at one time, and no temporary files are used,
75the effective size of file that can be edited is limited only
76by the requirement that the input and output fit simultaneously
77into available secondary storage.
78.LP
79Complicated editing scripts can be created separately and given
80to
81.ul
82sed
83as a command file.
84For complex edits, this saves considerable typing, and its
85attendant errors.
86.ul
87Sed
88running from a command file is much more efficient than any interactive
89editor known to the author, even if that editor
90can be driven by a pre-written script.
91.LP
92The principal loss of functions compared to an interactive editor
93are lack of relative addressing (because of the line-at-a-time
94operation), and lack of immediate verification that a command has
95done what was intended.
96.LP
97.ul
98Sed
99is a lineal descendant of the UNIX editor,
100.ul
101ed.
102Because of the differences between interactive and non-interactive
103operation, considerable changes have been made between
104.ul
105ed
106and
107.ul
108sed;
109even confirmed users of
110.ul
111ed
112will frequently be surprised (and probably chagrined),
113if they rashly use
114.ul
115sed
116without reading Sections 2 and 3 of this document.
117The most striking family resemblance between the two
118editors is in the class of patterns (`regular expressions') they
119recognize;
120the code for matching patterns is copied almost
121verbatim from the code for
122.ul
123ed,
124and the description of regular expressions in Section 2
125is copied almost verbatim from the UNIX Programmer's
126Manual[1]. (Both code and description were written by Dennis
127M. Ritchie.)
128.LP
129.SH
1301. Overall Operation
131.LP
132.ul
133Sed
134by default copies the standard input to the standard output,
135perhaps performing one or more editing commands on each
136line before writing it to the output.
137This behavior may be modified by flags on the command line;
138see Section 1.1 below.
139.LP
140The general format of an editing command is:
141.LP
142.in +1i
143[address1,address2][function][arguments]
144.LP
145.in -1i
146One or both addresses may be omitted; the format of addresses is
147given in Section 2.
148Any number of blanks or tabs may separate the addresses
149from the function.
150The function must be present; the available commands are discussed
151in Section 3.
152The arguments may be required or optional, according to which function
153is given; again, they are discussed in Section 3 under each individual
154function.
155.LP
156Tab characters and spaces at the beginning of lines are ignored.
157.LP
158.SH
1591.1. Command-line Flags
160.LP
161Three flags are recognized on the command line:
162.in +1i
163.ti -.5i
164.B
165-n:
166.R
167tells
168.ul
169sed
170not to copy all lines, but only those specified by
171.ul
172p
173functions or
174.ul
175p
176flags after
177.ul
178s
179functions (see Section 3.3);
180.ti -.5i
181.B
182-e:
183.R
184tells
185.ul
186sed
187to take the next argument as an editing command;
188.ti -.5i
189.B
190-f:
191.R
192tells
193.ul
194sed
195to take the next argument as a file name;
196the file should contain editing commands, one to a line.
197.in -1i
198.SH
1991.2. Order of Application of Editing Commands
200.LP
201Before any editing is done (in fact, before any input file is
202even opened), all the editing commands are compiled into
203a form which will be moderately efficient during
204the execution phase (when the commands are actually applied to
205lines of the input file).
206The commands are compiled in the order in which they are
207encountered; this is generally the order in which they will
208be attempted at execution time.
209The commands are applied one at a time; the input to each command
210is the output of all preceding commands.
211.LP
212The default linear order of application of editing commands can
213be changed by the flow-of-control commands,
214.ul
215t
216and
217.ul
218b
219(see Section 3).
220Even when the order of application is changed
221by these commands, it is still true that the input line to any
222command is the output of any previously applied command.
223.SH
2241.3. Pattern-space
225.LP
226The range of pattern matches is called the pattern space.
227Ordinarily, the pattern space is one line of the input text,
228but more than one line can be read into the pattern space
229by using the
230.ul
231N
232command (Section 3.6.).
233.LP
234.SH
2351.4. Examples
236.LP
237Examples are scattered throughout the text.
238Except where otherwise noted,
239the examples all assume the following input text:
240.LP
241.in +.5i
242.nf
243In Xanadu did Kubla Khan
244A stately pleasure dome decree:
245Where Alph, the sacred river, ran
246Through caverns measureless to man
247Down to a sunless sea.
248.in -.5i
249.fi
250.LP
251(In no case is the output of the
252.ul
253sed
254commands to be considered an improvement
255on Coleridge.)
256.LP
257.SH
258Example:
259.LP
260The command
261.in +.5i
262.LP
2632q
264.in -.5i
265.LP
266will quit after copying the first two lines of the input.
267The output will be:
268.LP
269.in +.5i
270.nf
271In Xanadu did Kubla Khan
272A stately pleasure dome decree:
273.in -.5i
274.fi
275.LP
276.SH
2772. ADDRESSES: Selecting lines for editing
278.LP
279Lines in the input file(s) to which editing commands are
280to be applied can be selected by addresses.
281Addresses may be either line numbers or context addresses.
282.LP
283The application of a group of commands can be controlled by
284one address (or address-pair) by grouping
285the commands with curly braces (`{ }')(Sec. 3.6.).
286.SH
2872.1. Line-number Addresses
288.LP
289A line number is a decimal integer.
290As each line is read from the input, a line-number counter
291is incremented;
292a line-number address matches (selects) the input
293line which causes the internal counter to equal the
294address line-number.
295The counter runs cumulatively through multiple input files;
296it is not reset when a new input file is opened.
297.LP
298As a special case, the character
299$
300matches the last line of the last input file.
301.SH
3022.2. Context Addresses
303.LP
304A context address is a pattern (`regular expression') enclosed in slashes (`/').
305The regular expressions recognized by
306.ul
307sed
308are constructed as follows:
309.in +1i
310.LP
311.ti -.5i
3121)
313An ordinary character (not one of those discussed below)
314is a regular expression, and matches that character.
315.LP
316.ti -.5i
3172)
318A circumflex `^' at the beginning of a regular expression
319matches the null character at the beginning of a line.
320.ti -.5i
3213)
322A dollar-sign `$' at the end of a regular expression
323matches the null character at the end of a line.
324.ti -.5i
3254)
326The characters `\en' match an imbedded newline character,
327but not the newline at the end of the pattern space.
328.ti -.5i
3295)
330A period `.' matches any character except the terminal newline
331of the pattern space.
332.ti -.5i
3336)
334A regular expression followed by an asterisk `*' matches any
335number (including 0) of adjacent occurrences of the regular
336expression it follows.
337.ti -.5i
3387)
339A string of characters in square brackets `[ ]' matches any character
340in the string, and no others.
341If, however, the first character of the string is circumflex `^',
342the regular expression matches any character
343.ul
344except
345the characters in the string and the terminal newline of the pattern space.
346.ti -.5i
3478)
348A concatenation of regular expressions is a regular expression
349which matches the concatenation of strings matched by the
350components of the regular expression.
351.ti -.5i
3529)
353A regular expression between the sequences `\e(' and `\e)' is
354identical in effect to the unadorned regular expression, but has
355side-effects which are described under the
356.ul
357s
358command below and specification 10) immediately below.
359.ti -.5i
36010)
361The expression
362.ul
363`\|\ed'
364means the same string of characters matched
365by an expression enclosed in `\e(' and `\e)'
366earlier in the same pattern.
367Here
368.ul
369d
370is a single digit;
371the string specified is that beginning with the
372\fId\|\fRth
373occurrence of `\e(' counting from the left.
374For example, the expression
375`^\e(.*\e)\e1' matches a line beginning with
376two repeated occurrences of the same string.
377.ti -.5i
37811)
379The null regular expression standing alone (e.g., `//') is
380equivalent to the last regular expression compiled.
381.in -1i
382.LP
383To use one of the special characters (^ $ . * [ ] \e /) as a literal
384(to match an occurrence of itself in the input), precede the
385special character by a backslash `\e'.
386.LP
387For a context address to `match' the input requires that
388the whole pattern within the address match some
389portion of the pattern space.
390.SH
3912.3. Number of Addresses
392.LP
393The commands in the next section can have 0, 1, or 2 addresses.
394Under each command the maximum number of allowed addresses is
395given.
396For a command to have more addresses than the maximum allowed
397is considered an error.
398.LP
399If a command has no addresses, it is applied to every line
400in the input.
401.LP
402If a command has one address, it is applied to all
403lines which match that address.
404.LP
405If a command has two addresses, it is applied to the first
406line which matches the first address, and to all subsequent lines
407until (and including) the first subsequent line which matches
408the second address.
409Then an attempt is made on subsequent lines to again match the first
410address, and the process is repeated.
411.LP
412Two addresses are separated by a comma.
413.SH
414Examples:
415.LP
416.nf
417.in +.5i
418.ta 1i 2i
419/an/ matches lines 1, 3, 4 in our sample text
420/an.*an/ matches line 1
421/^an/ matches no lines
422/./ matches all lines
423/\e./ matches line 5
424/r*an/ matches lines 1,3, 4 (number = zero!)
425/\e(an\e).*\e1/ matches line 1
426.fi
427.in 0
428.LP
429.SH
4303. FUNCTIONS
431.LP
432All functions are named by a single character.
433In the following summary, the maximum number of allowable addresses
434is given enclosed in parentheses, then the single character
435function name, possible arguments enclosed in angles (< >),
436an expanded English translation of the single-character name,
437and finally a description of what each function does.
438The angles around the arguments are
439.ul
440not
441part of the argument, and should not be typed
442in actual editing commands.
443.SH
4443.1. Whole-line Oriented Functions
445.LP
446.in +1i
447.ti -.5i
448(2)d -- delete lines
449.if t .sp .5
450The
451.ul
452d
453function deletes from the file (does not write to the output)
454all those lines matched by its address(es).
455.if t .sp .5
456It also has the side effect that no further commands are attempted
457on the corpse of a deleted line;
458as soon as the
459.ul
460d
461function is executed, a new line is read from the input, and
462the list of editing commands is re-started from the beginning
463on the new line.
464.ti -.5i
465.if t .sp .5
466(2)n -- next line
467.if t .sp .5
468The
469.ul
470n
471function reads the next line from the input, replacing
472the current line.
473The current line is written to the output if it should
474be.
475The list of editing commands is continued
476following the
477.ul
478n
479command.
480.if t .sp .5
481.nf
482.in -.5i
483(1)a\e
484<text> -- append lines
485.in +.5i
486.fi
487.if t .sp .5
488The
489.ul
490a
491function causes the argument <text> to be written to the
492output after the line matched by its address.
493The
494.ul
495a
496command is inherently multi-line;
497.ul
498a
499must appear at the end of a line, and <text> may contain
500any number of lines.
501To preserve the one-command-to-a-line fiction,
502the interior newlines must be hidden by a
503backslash character (`\e') immediately preceding the
504newline.
505The <text> argument is terminated by the first unhidden
506newline (the first one not immediately preceded
507by backslash).
508.if t .sp .5
509Once an
510.ul
511a
512function is successfully executed, <text> will be
513written to the output regardless of what later commands do to
514the line which triggered it.
515The triggering line may be
516deleted entirely; <text> will still be written to the output.
517.if t .sp .5
518The <text> is not scanned for address matches, and no editing
519commands are attempted on it.
520It does not cause any change in the line-number counter.
521.if t .sp .5
522.nf
523.in -.5i
524(1)i\e
525<text> -- insert lines
526.in +.5i
527.fi
528.if t .sp .5
529The
530.ul
531i
532function behaves identically to the
533.ul
534a
535function, except that <text> is written to the output
536.ul
537before
538the matched line.
539All other comments about the
540.ul
541a
542function apply to the
543.ul
544i
545function as well.
546.if t .sp .5
547.nf
548.in -.5i
549(2)c\e
550<text> -- change lines
551.in +.5i
552.fi
553.if t .sp .5
554The
555.ul
556c
557function deletes the lines selected by its address(es),
558and replaces them with the lines in <text>.
559Like
560.ul
561a
562and
563.ul
564i,
565.ul
566c
567must be followed by a newline hidden by a backslash;
568and interior new lines in <text> must be hidden by
569backslashes.
570.if t .sp .5
571The
572.ul
573c
574command may have two addresses, and therefore select a range
575of lines.
576If it does, all the lines in the range are deleted, but only
577one copy of <text> is written to the output,
578.ul
579not
580one copy per line deleted.
581As with
582.ul
583a
584and
585.ul
586i,
587<text> is not scanned for address matches, and no
588editing commands are attempted on it.
589It does not change the line-number counter.
590.if t .sp .5
591After a line has been deleted by a
592.ul
593c
594function, no further commands are attempted on the corpse.
595.if t .sp .5
596If text is appended after a line by
597.ul
598a
599or
600.ul
601r
602functions, and the line is subsequently changed, the text
603inserted by the
604.ul
605c
606function will be placed
607.ul
608before
609the text of the
610.ul
611a
612or
613.ul
614r
615functions.
616(The
617.ul
618r
619function is described in Section 3.4.)
620.if t .sp .5
621.in -1i
622.ul
623Note:
624Within the text put in the output by these functions,
625leading blanks and tabs will disappear, as always in
626.ul
627sed
628commands.
629To get leading blanks and tabs into the output, precede the first
630desired blank or tab by a backslash; the backslash will not
631appear in the output.
632.SH
633Example:
634.LP
635The list of editing commands:
636.LP
637.in +.5i
638.nf
639n
640a\e
641XXXX
642d
643.in -.5i
644.fi
645.LP
646applied to our standard input, produces:
647.LP
648.in +.5i
649.nf
650In Xanadu did Kubhla Khan
651XXXX
652Where Alph, the sacred river, ran
653XXXX
654Down to a sunless sea.
655.in -.5i
656.fi
657.LP
658In this particular case,
659the same effect would be produced by either
660of the two following command lists:
661.LP
662.in +.5i
663.nf
664n n
665i\e c\e
666XXXX XXXX
667d
668.in -.5i
669.fi
670.LP
671.in 0
672.SH
6733.2. Substitute Function
674.LP
675One very important function changes parts of lines selected by
676a context search within the line.
677.if t .sp .5
678.in +1i
679.ti -.5i
680(2)s<pattern><replacement><flags> -- substitute
681.if t .sp .5
682The
683.ul
684s
685function replaces
686.ul
687part
688of a line (selected by <pattern>) with <replacement>.
689It can best be read:
690.if t .sp .5
691.ti +1i
692Substitute for <pattern>, <replacement>
693.if t .sp .5
694The <pattern> argument contains a pattern,
695exactly like the patterns in addresses (see 2.2 above).
696The only difference between <pattern> and a context address is
697that the context address must be delimited by slash (`/') characters;
698<pattern> may be delimited by any character other than space or
699newline.
700.if t .sp .5
701By default, only the first string matched by <pattern> is replaced,
702but see the
703.ul
704g
705flag below.
706.if t .sp .5
707The
708<replacement> argument begins immediately after the
709second delimiting character of <pattern>, and must be followed
710immediately by another instance of the delimiting character.
711(Thus there are exactly
712.ul
713three
714instances of the delimiting character.)
715.if t .sp .5
716The <replacement> is not a pattern,
717and the characters which are special in patterns
718do not have special meaning in <replacement>.
719Instead, other characters are special:
720.if t .sp .5
721.in +1i
722.ti -.5i
723& is replaced by the string matched by <pattern>
724.if t .sp .5
725.ti -.5i
726.ul
727\ed
728(where
729.ul
730d
731is a single digit) is replaced by the \fId\fRth substring
732matched by parts of <pattern> enclosed in `\e(' and `\e)'.
733If nested substrings occur in <pattern>, the \fId\fRth
734is determined by counting opening delimiters (`\e(').
735.if t .sp .5
736As in patterns, special characters may be made
737literal by preceding them with backslash (`\e').
738.if t .sp .5
739.in -1i
740The <flags> argument may contain the following flags:
741.if t .sp .5
742.in +1i
743.ti -.5i
744g -- substitute <replacement> for all (non-overlapping)
745instances of <pattern> in the line.
746After a successful substitution, the scan for the next
747instance of <pattern> begins just after the end of the
748inserted characters; characters put into the line from
749<replacement> are not rescanned.
750.if t .sp .5
751.ti -.5i
752p -- print the line if a successful replacement was done.
753The
754.ul
755p
756flag causes the line to be written to the output if and only
757if a substitution was actually made by the
758.ul
759s
760function.
761Notice that if several
762.ul
763s
764functions, each followed by a
765.ul
766p
767flag, successfully substitute in the same input line,
768multiple copies of the line will be written to the
769output: one for each successful substitution.
770.if t .sp .5
771.ti -.5i
772w <filename> -- write the line to a file if a successful
773replacement was done.
774The
775.ul
776w
777flag causes lines which are actually substituted by the
778.ul
779s
780function to be written to a file named by <filename>.
781If <filename> exists before
782.ul
783sed
784is run, it is overwritten;
785if not, it is created.
786.if t .sp .5
787A single space must separate
788.ul
789w
790and <filename>.
791.if t .sp .5
792The possibilities of multiple, somewhat different copies of
793one input line being written are the same as for
794.ul
795p.
796.if t .sp .5
797A maximum of 10 different file names may be mentioned after
798.ul
799w
800flags and
801.ul
802w
803functions (see below), combined.
804.in 0
805.SH
806Examples:
807.LP
808The following command, applied to our standard input,
809.LP
810.in +.5i
811s/to/by/w changes
812.in -.5i
813.LP
814produces, on the standard output:
815.LP
816.in +.5i
817.nf
818In Xanadu did Kubhla Khan
819A stately pleasure dome decree:
820Where Alph, the sacred river, ran
821Through caverns measureless by man
822Down by a sunless sea.
823.fi
824.in -.5i
825.LP
826and, on the file `changes':
827.LP
828.in +.5i
829.nf
830Through caverns measureless by man
831Down by a sunless sea.
832.fi
833.in -.5i
834.LP
835If the nocopy option is in effect, the command:
836.LP
837.in +.5i
838.nf
839s/[.,;?:]/*P&*/gp
840.fi
841.in -.5i
842.LP
843produces:
844.LP
845.in +.5i
846.nf
847A stately pleasure dome decree*P:*
848Where Alph*P,* the sacred river*P,* ran
849Down to a sunless sea*P.*
850.LP
851.in -.5i
852.fi
853Finally, to illustrate the effect of the
854.ul
855g
856flag,
857the command:
858.LP
859.in +.5i
860.nf
861/X/s/an/AN/p
862.in -.5i
863.fi
864.LP
865produces (assuming nocopy mode):
866.in +.5i
867.LP
868.nf
869In XANadu did Kubhla Khan
870.fi
871.in -.5i
872.LP
873and the command:
874.LP
875.in +.5i
876.nf
877/X/s/an/AN/gp
878.in -.5i
879.fi
880.LP
881produces:
882.LP
883.in +.5i
884.nf
885In XANadu did Kubhla KhAN
886.in -.5i
887.fi
888.LP
889.in 0
890.SH
8913.3. Input-output Functions
892.LP
893.in +1i
894.ti -.5i
895(2)p -- print
896.if t .sp .5
897The print function writes the addressed lines to the standard output file.
898They are written at the time the
899.ul
900p
901function is encountered, regardless of what succeeding
902editing commands may do to the lines.
903.if t .sp .5
904.ti -.5i
905(2)w <filename> -- write on <filename>
906.if t .sp .5
907The write function writes the addressed lines to the file named
908by <filename>.
909If the file previously existed, it is overwritten; if not, it is created.
910The lines are written exactly as they exist when the write function
911is encountered for each line, regardless of what subsequent
912editing commands may do to them.
913.if t .sp .5
914Exactly one space must separate the
915.ul
916w
917and <filename>.
918.if t .sp .5
919A maximum of ten different files may be mentioned in write
920functions and
921.ul
922w
923flags after
924.ul
925s
926functions, combined.
927.if t .sp .5
928.ti -.5i
929(1)r <filename> -- read the contents of a file
930.if t .sp .5
931The read function reads the contents of <filename>, and appends
932them after the line matched by the address.
933The file is read and appended regardless of what subsequent
934editing commands do to the line which matched its address.
935If
936.ul
937r
938and
939.ul
940a
941functions are executed on the same line,
942the text from the
943.ul
944a
945functions and the
946.ul
947r
948functions is written to the output in the order that
949the functions are executed.
950.if t .sp .5
951Exactly one space must separate the
952.ul
953r
954and <filename>.
955If a file mentioned by a
956.ul
957r
958function cannot be opened, it is considered a null file,
959not an error, and no diagnostic is given.
960.if t .sp .5
961.in -1i
962NOTE:
963Since there is a limit to the number of files that can be opened
964simultaneously, care should be taken that no more than ten
965files be mentioned in
966.ul
967w
968functions or flags; that number is reduced by one if any
969.ul
970r
971functions are present.
972(Only one read file is open at one time.)
973.in 0
974.SH
975Examples
976.LP
977Assume that the file `note1'
978has the following contents:
979.LP
980.in +1i
981Note: Kubla Khan (more properly Kublai Khan; 1216-1294)
982was the grandson and most eminent successor of Genghiz
983(Chingiz) Khan, and founder of the Mongol dynasty in China.
984.LP
985.in 0
986Then the following command:
987.LP
988.nf
989.in +.5i
990/Kubla/r note1
991.in -.5i
992.fi
993.LP
994produces:
995.LP
996.nf
997.in +.5i
998In Xanadu did Kubla Khan
999.in +.5i
1000.fi
1001Note: Kubla Khan (more properly Kublai Khan; 1216-1294)
1002was the grandson and most eminent successor of Genghiz
1003(Chingiz) Khan, and founder of the Mongol dynasty in China.
1004.in -.5i
1005.nf
1006A stately pleasure dome decree:
1007Where Alph, the sacred river, ran
1008Through caverns measureless to man
1009Down to a sunless sea.
1010.in -.5i
1011.fi
1012.LP
1013.in 0
1014.SH
10153.4.
1016Multiple Input-line Functions
1017.LP
1018Three functions, all spelled with capital letters, deal
1019specially with pattern spaces containing imbedded newlines;
1020they are intended principally to provide pattern matches across
1021lines in the input.
1022.if t .sp .5
1023.in +1i
1024.ti -.5i
1025(2)N -- Next line
1026.if t .sp .5
1027The next input line is appended to the current line in the
1028pattern space; the two input lines are separated by an imbedded
1029newline.
1030Pattern matches may extend across the imbedded newline(s).
1031.if t .sp .5
1032.ti -.5i
1033(2)D -- Delete first part of the pattern space
1034.if t .sp .5
1035Delete up to and including the first newline character
1036in the current pattern space.
1037If the pattern space becomes empty (the only newline
1038was the terminal newline),
1039read another line from the input.
1040In any case, begin the list of editing commands again
1041from its beginning.
1042.if t .sp .5
1043.ti -.5i
1044(2)P -- Print first part of the pattern space
1045.if t .sp .5
1046Print up to and including the first newline in the pattern space.
1047.if t .sp .5
1048.in 0
1049The
1050.ul
1051P
1052and
1053.ul
1054D
1055functions are equivalent to their lower-case counterparts
1056if there are no imbedded newlines in the pattern space.
1057.in 0
1058.SH
10593.5. Hold and Get Functions
1060.LP
1061Four functions save and retrieve part of the input for possible later
1062use.
1063.if t .sp .5
1064.in 1i
1065.ti -.5i
1066(2)h -- hold pattern space
1067.if t .sp .5
1068The
1069.ul
1070h
1071functions copies the contents of the pattern space
1072into a hold area (destroying the previous contents of the
1073hold area).
1074.if t .sp .5
1075.ti -.5i
1076(2)H -- Hold pattern space
1077.if t .sp .5
1078The
1079.ul
1080H
1081function appends the contents of the pattern space
1082to the contents of the hold area; the former and new contents
1083are separated by a newline.
1084.if t .sp .5
1085.ti -.5i
1086(2)g -- get contents of hold area
1087.if t .sp .5
1088The
1089.ul
1090g
1091function copies the contents of the hold area into
1092the pattern space (destroying the previous contents of the
1093pattern space).
1094.if t .sp .5
1095.ti -.5i
1096(2)G -- Get contents of hold area
1097.if t .sp .5
1098The
1099.ul
1100G
1101function appends the contents of the hold area to the
1102contents of the pattern space; the former and new contents are separated by
1103a newline.
1104.if t .sp .5
1105.ti -.5i
1106(2)x -- exchange
1107.if t .sp .5
1108The exchange command interchanges the contents
1109of the pattern space and the hold area.
1110.in 0
1111.SH
1112Example
1113.LP
1114The commands
1115.nf
1116.if t .sp .5
1117 1h
1118 1s/ did.*//
1119 1x
1120 G
1121 s/\en/ :/
1122.if t .sp .5
1123.fi
1124applied to our standard example, produce:
1125.nf
1126.if t .sp .5
1127 In Xanadu did Kubla Khan :In Xanadu
1128 A stately pleasure dome decree: :In Xanadu
1129 Where Alph, the sacred river, ran :In Xanadu
1130 Through caverns measureless to man :In Xanadu
1131 Down to a sunless sea. :In Xanadu
1132.if t .sp .5
1133.fi
1134.SH
11353.6. Flow-of-Control Functions
1136.LP
1137These functions do no editing on the input
1138lines, but control the application of functions
1139to the lines selected by the address part.
1140.if t .sp .5
1141.in +1i
1142.ti -.5i
1143(2)! -- Don't
1144.if t .sp .5
1145The
1146.ul
1147Don't
1148command causes the next command
1149(written on the same line), to be applied to all and only those input lines
1150.ul
1151not
1152selected by the adress part.
1153.if t .sp .5
1154.ti -.5i
1155(2){ -- Grouping
1156.if t .sp .5
1157The grouping command `{' causes the
1158next set of commands to be applied
1159(or not applied) as a block to the
1160input lines selected by the addresses
1161of the grouping command.
1162The first of the commands under control of the grouping
1163may appear on the same line as the
1164`{' or on the next line.
1165.LP
1166The group of commands is terminated by a
1167matching `}' standing on a line by itself.
1168.LP
1169Groups can be nested.
1170.ti -.5i
1171.if t .sp .5
1172(0):<label> -- place a label
1173.if t .sp .5
1174The label function marks a place in the list
1175of editing commands which may be referred to by
1176.ul
1177b
1178and
1179.ul
1180t
1181functions.
1182The <label> may be any sequence of eight or fewer characters;
1183if two different colon functions have identical labels,
1184a compile time diagnostic will be generated, and
1185no execution attempted.
1186.if t .sp .5
1187.ti -.5i
1188(2)b<label> -- branch to label
1189.if t .sp .5
1190The branch function causes the sequence of editing commands being
1191applied to the current input line to be restarted immediately
1192after the place where a colon function with the same <label>
1193was encountered.
1194If no colon function with the same label can be found after
1195all the editing commands have been compiled, a compile time diagnostic
1196is produced, and no execution is attempted.
1197.if t .sp .5
1198A
1199.ul
1200b
1201function with no <label> is taken to be a branch to the end of the
1202list of editing commands;
1203whatever should be done with the current input line is done, and
1204another input line is read; the list of editing commands is restarted from the
1205beginning on the new line.
1206.if t .sp .5
1207.ti -.5i
1208(2)t<label> -- test substitutions
1209.if t .sp .5
1210The
1211.ul
1212t
1213function tests whether
1214.ul
1215any
1216successful substitutions have been made on the current input
1217line;
1218if so, it branches to <label>;
1219if not, it does nothing.
1220The flag which indicates that a successful substitution has
1221been executed is reset by:
1222.if t .sp .5
1223.in +1i
12241) reading a new input line, or
1225.br
12262) executing a
1227.ul
1228t
1229function.
1230.if t .sp .5
1231.in 0
1232.SH
12333.7. Miscellaneous Functions
1234.LP
1235.in +1i
1236.ti -.5i
1237(1)= -- equals
1238.if t .sp .5
1239The = function writes to the standard output the line number of the
1240line matched by its address.
1241.if t .sp .5
1242.ti -.5i
1243(1)q -- quit
1244.if t .sp .5
1245The
1246.ul
1247q
1248function causes the current line to be written to the
1249output (if it should be), any appended or read text to be written, and
1250execution to be terminated.
1251.in 0
1252.SH
1253.SH
1254Reference
1255.IP [1]
1256Ken Thompson and Dennis M. Ritchie,
1257.ul
1258The UNIX Programmer's Manual.
1259Bell Laboratories, 1978.