home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / datafile / usd / 18_sed / sed_txt
Encoding:
Text File  |  1996-10-12  |  28.0 KB  |  859 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.             SED -- A Non-interactive Text Editor
  11.  
  12.                        Lee E. McMahon
  13.  
  14.  
  15.  
  16.  
  17.                           ABSTRACT
  18.  
  19.  
  20.           Sed  is a non-interactive context editor that
  21.      runs on the  UNIX(R)  operating  system.   Sed  is
  22.      designed to be especially useful in three cases:
  23.  
  24. 1)  To edit files too large for comfortable interactive
  25.      editing;
  26. 2) To edit any size file when the sequence  of  editing
  27.      commands  is  too  complicated  to  be comfortably
  28.      typed in interactive mode.
  29. 3) To perform multiple `global' editing functions effi-
  30.      ciently in one pass through the input.
  31.  
  32.      This  memorandum constitutes a manual for users of
  33.      sed.
  34.  
  35.  
  36. Introduction
  37.  
  38. Sed is a non-interactive context editor designed to be espe-
  39. cially useful in three cases:
  40.  
  41. 1) To edit files too large for comfortable interactive edit-
  42. ing;
  43. 2) To edit any size file when the sequence of  editing  com-
  44. mands is too complicated to be comfortably typed in interac-
  45. tive mode;
  46. 3) To perform  multiple  `global'  editing  functions  effi-
  47. ciently in one pass through the input.
  48.  
  49. Since  only  a  few lines of the input reside in core at one
  50. time, and no temporary files are used, the effective size of
  51. file  that  can be edited is limited only by the requirement
  52. that the input and output fit simultaneously into  available
  53. secondary storage.
  54.  
  55. Complicated  editing  scripts  can be created separately and
  56. given to sed as a command file.   For  complex  edits,  this
  57. saves  considerable  typing,  and its attendant errors.  Sed
  58. running from a command file is much more efficient than  any
  59. interactive  editor known to the author, even if that editor
  60. can be driven by a pre-written script.
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. USD:18-2                SED -- A Non-interactive Text Editor
  71.  
  72.  
  73. The principal loss of functions compared to  an  interactive
  74. editor are lack of relative addressing (because of the line-
  75. at-a-time operation), and  lack  of  immediate  verification
  76. that a command has done what was intended.
  77.  
  78. Sed  is a lineal descendant of the UNIX editor, ed.  Because
  79. of the differences between interactive  and  non-interactive
  80. operation,  considerable  changes  have been made between ed
  81. and sed; even confirmed users of ed will frequently be  sur-
  82. prised  (and  probably  chagrined),  if  they rashly use sed
  83. without reading Sections 2 and 3 of this document.  The most
  84. striking  family  resemblance  between the two editors is in
  85. the class of patterns (`regular  expressions')  they  recog-
  86. nize; the code for matching patterns is copied almost verba-
  87. tim from the code for ed, and  the  description  of  regular
  88. expressions  in Section 2 is copied almost verbatim from the
  89. UNIX Programmer's Manual[1]. (Both code and description were
  90. written by Dennis M. Ritchie.)
  91.  
  92.  
  93. 1. Overall Operation
  94.  
  95. Sed  by  default  copies  the standard input to the standard
  96. output, perhaps performing one or more editing  commands  on
  97. each  line  before  writing it to the output.  This behavior
  98. may be modified by flags on the command  line;  see  Section
  99. 1.1 below.
  100.  
  101. The general format of an editing command is:
  102.  
  103.           [address1,address2][function][arguments]
  104.  
  105. One  or  both  addresses  may  be  omitted;  the  format  of
  106. addresses is given in Section 2.  Any number  of  blanks  or
  107. tabs  may  separate  the  addresses  from the function.  The
  108. function must be present; the available  commands  are  dis-
  109. cussed  in  Section  3.   The  arguments  may be required or
  110. optional, according to which function is given; again,  they
  111. are discussed in Section 3 under each individual function.
  112.  
  113. Tab  characters  and  spaces  at  the beginning of lines are
  114. ignored.
  115.  
  116.  
  117. 1.1. Command-line Flags
  118.  
  119. Three flags are recognized on the command line:
  120.      -n: tells sed not to copy all  lines,  but  only  those
  121.           specified  by p functions or p flags after s func-
  122.           tions (see Section 3.3);
  123.      -e: tells sed to take the next argument as  an  editing
  124.           command;
  125.      -f: tells sed to take the next argument as a file name;
  126.           the file should contain editing commands, one to a
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. SED -- A Non-interactive Text Editor                USD:18-3
  137.  
  138.  
  139.           line.
  140.  
  141. 1.2. Order of Application of Editing Commands
  142.  
  143. Before  any  editing is done (in fact, before any input file
  144. is even opened), all the editing commands are compiled  into
  145. a  form which will be moderately efficient during the execu-
  146. tion phase (when the commands are actually applied to  lines
  147. of  the input file).  The commands are compiled in the order
  148. in which they are encountered; this is generally  the  order
  149. in which they will be attempted at execution time.  The com-
  150. mands are applied one at a time; the input to  each  command
  151. is the output of all preceding commands.
  152.  
  153. The  default linear order of application of editing commands
  154. can be changed by the flow-of-control commands, t and b (see
  155. Section  3).   Even when the order of application is changed
  156. by these commands, it is still true that the input  line  to
  157. any command is the output of any previously applied command.
  158.  
  159. 1.3.  Pattern-space
  160.  
  161. The range of pattern matches is called  the  pattern  space.
  162. Ordinarily, the pattern space is one line of the input text,
  163. but more than one line can be read into the pattern space by
  164. using the N command (Section 3.6.).
  165.  
  166.  
  167. 1.4. Examples
  168.  
  169. Examples  are  scattered  throughout the text.  Except where
  170. otherwise noted, the examples all assume the following input
  171. text:
  172.  
  173.      In Xanadu did Kubla Khan
  174.      A stately pleasure dome decree:
  175.      Where Alph, the sacred river, ran
  176.      Through caverns measureless to man
  177.      Down to a sunless sea.
  178.  
  179. (In  no case is the output of the sed commands to be consid-
  180. ered an improvement on Coleridge.)
  181.  
  182.  
  183. Example:
  184.  
  185. The command
  186.  
  187. 2q
  188.  
  189. will quit after copying the first two lines  of  the  input.
  190. The output will be:
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. USD:18-4                SED -- A Non-interactive Text Editor
  203.  
  204.  
  205.      In Xanadu did Kubla Khan
  206.      A stately pleasure dome decree:
  207.  
  208.  
  209. 2. ADDRESSES: Selecting lines for editing
  210.  
  211. Lines  in the input file(s) to which editing commands are to
  212. be applied can be selected by addresses.  Addresses  may  be
  213. either line numbers or context addresses.
  214.  
  215. The  application of a group of commands can be controlled by
  216. one address (or address-pair) by grouping the commands  with
  217. curly braces (`{ }')(Sec. 3.6.).
  218.  
  219. 2.1. Line-number Addresses
  220.  
  221. A  line  number  is a decimal integer.  As each line is read
  222. from the input, a  line-number  counter  is  incremented;  a
  223. line-number  address  matches (selects) the input line which
  224. causes the internal  counter  to  equal  the  address  line-
  225. number.   The  counter  runs  cumulatively  through multiple
  226. input files; it is not  reset  when  a  new  input  file  is
  227. opened.
  228.  
  229. As  a special case, the character $ matches the last line of
  230. the last input file.
  231.  
  232. 2.2. Context Addresses
  233.  
  234. A  context  address  is  a  pattern  (`regular  expression')
  235. enclosed  in  slashes (`/').  The regular expressions recog-
  236. nized by sed are constructed as follows:
  237.  
  238. 1) An ordinary character (not one of those discussed  below)
  239. is a regular expression, and matches that character.
  240.  
  241. 2) A circumflex `^' at the beginning of a regular expression
  242. matches the null character at the beginning of a line.
  243. 3) A dollar-sign `$' at the  end  of  a  regular  expression
  244. matches the null character at the end of a line.
  245. 4)  The characters `\n' match an imbedded newline character,
  246. but not the newline at the end of the pattern space.
  247. 5) A period `.' matches any character  except  the  terminal
  248. newline of the pattern space.
  249. 6)  A regular expression followed by an asterisk `*' matches
  250. any number (including 0) of adjacent occurrences of the reg-
  251. ular expression it follows.
  252. 7)  A  string of characters in square brackets `[ ]' matches
  253. any character in the string, and no  others.   If,  however,
  254. the  first  character  of  the string is circumflex `^', the
  255. regular expression matches any character except the  charac-
  256. ters  in  the string and the terminal newline of the pattern
  257. space.
  258. 8) A concatenation  of  regular  expressions  is  a  regular
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. SED -- A Non-interactive Text Editor                USD:18-5
  269.  
  270.  
  271. expression   which  matches  the  concatenation  of  strings
  272. matched by the components of the regular expression.
  273. 9) A regular expression between the sequences `\(' and  `\)'
  274. is  identical in effect to the unadorned regular expression,
  275. but has side-effects which are described under the s command
  276. below and specification 10) immediately below.
  277. 10)  The expression `\d' means the same string of characters
  278. matched by an expression enclosed in `\(' and  `\)'  earlier
  279. in  the  same pattern.  Here d is a single digit; the string
  280. specified is that beginning with the dth occurrence of  `\('
  281. counting   from  the  left.   For  example,  the  expression
  282. `^\(.*\)\1' matches  a  line  beginning  with  two  repeated
  283. occurrences of the same string.
  284. 11)  The null regular expression standing alone (e.g., `//')
  285. is equivalent to the  last regular expression compiled.
  286.  
  287. To use one of the special characters (^ $ . * [ ] \ /) as  a
  288. literal  (to  match  an  occurrence of itself in the input),
  289. precede the special character by a backslash `\'.
  290.  
  291. For a context address to `match' the input requires that the
  292. whole  pattern  within the address match some portion of the
  293. pattern space.
  294.  
  295. 2.3. Number of Addresses
  296.  
  297. The commands in the  next  section  can  have  0,  1,  or  2
  298. addresses.  Under each command the maximum number of allowed
  299. addresses is given.  For a command to  have  more  addresses
  300. than the maximum allowed is considered an error.
  301.  
  302. If  a  command has no addresses, it is applied to every line
  303. in the input.
  304.  
  305. If a command has one address, it is  applied  to  all  lines
  306. which match that address.
  307.  
  308. If  a  command has two addresses, it is applied to the first
  309. line which matches the first address, and to all  subsequent
  310. lines  until (and including) the first subsequent line which
  311. matches the second address.  Then an attempt is made on sub-
  312. sequent lines to again match the first address, and the pro-
  313. cess is repeated.
  314.  
  315. Two addresses are separated by a comma.
  316.  
  317. Examples:
  318.  
  319.      /an/      matches lines 1, 3, 4 in our sample text
  320.      /an.*an/  matches line 1
  321.      /^an/     matches no lines
  322.      /./       matches all lines
  323.      /\./      matches line 5
  324.      /r*an/    matches lines 1,3, 4 (number = zero!)
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. USD:18-6                SED -- A Non-interactive Text Editor
  335.  
  336.  
  337.      /\(an\).*\1/        matches line 1
  338.  
  339.  
  340. 3. FUNCTIONS
  341.  
  342. All functions are named by a single character.  In the  fol-
  343. lowing summary, the maximum number of allowable addresses is
  344. given enclosed in parentheses,  then  the  single  character
  345. function  name, possible arguments enclosed in angles (< >),
  346. an expanded  English  translation  of  the  single-character
  347. name,  and finally a description of what each function does.
  348. The angles around the arguments are not part  of  the  argu-
  349. ment, and should not be typed in actual editing commands.
  350.  
  351. 3.1. Whole-line Oriented Functions
  352.  
  353.      (2)d  --  delete  lines The d function deletes from the
  354.           file (does not write  to  the  output)  all  those
  355.           lines matched by its address(es).  It also has the
  356.           side effect that no further commands are attempted
  357.           on  the corpse of a deleted line; as soon as the d
  358.           function is executed, a new line is read from  the
  359.           input,  and  the  list  of editing commands is re-
  360.           started from the beginning on the new line.
  361.      (2)n -- next line The n function reads  the  next  line
  362.           from  the  input, replacing the current line.  The
  363.           current line is written to the output if it should
  364.           be.   The  list  of  editing commands is continued
  365.           following the n command.
  366.      (1)a\
  367.      <text> -- append lines
  368.           The a function causes the argument  <text>  to  be
  369.           written  to  the  output after the line matched by
  370.           its address.  The a command is  inherently  multi-
  371.           line;  a  must  appear  at  the end of a line, and
  372.           <text> may contain any number of lines.   To  pre-
  373.           serve the one-command-to-a-line fiction, the inte-
  374.           rior newlines must be hidden by a backslash  char-
  375.           acter  (`\')  immediately  preceding  the newline.
  376.           The <text> argument is  terminated  by  the  first
  377.           unhidden  newline  (the  first one not immediately
  378.           preceded by backslash).  Once  an  a  function  is
  379.           successfully  executed,  <text> will be written to
  380.           the output regardless of what later commands do to
  381.           the  line which triggered it.  The triggering line
  382.           may be deleted  entirely;  <text>  will  still  be
  383.           written  to the output.  The <text> is not scanned
  384.           for address matches, and no editing  commands  are
  385.           attempted  on it.  It does not cause any change in
  386.           the line-number counter.
  387.      (1)i\
  388.      <text> -- insert lines
  389.           The i function  behaves identically to the a func-
  390.           tion,  except that <text> is written to the output
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. SED -- A Non-interactive Text Editor                USD:18-7
  401.  
  402.  
  403.           before the matched line.  All other comments about
  404.           the a function apply to the i function as well.
  405.      (2)c\
  406.      <text> -- change lines
  407.           The  c  function deletes the lines selected by its
  408.           address(es), and replaces them with the  lines  in
  409.           <text>.   Like  a  and  i, c must be followed by a
  410.           newline hidden by a backslash;  and  interior  new
  411.           lines  in  <text>  must  be hidden by backslashes.
  412.           The c command may have two addresses,  and  there-
  413.           fore select a range of lines.  If it does, all the
  414.           lines in the range are deleted, but only one  copy
  415.           of  <text>  is written to the output, not one copy
  416.           per line deleted.  As with a and i, <text> is  not
  417.           scanned  for  address matches, and no editing com-
  418.           mands are attempted on it.  It does not change the
  419.           line-number   counter.   After  a  line  has  been
  420.           deleted by a c function, no further  commands  are
  421.           attempted  on  the  corpse.   If  text is appended
  422.           after a line by a or r functions, and the line  is
  423.           subsequently  changed,  the text inserted by the c
  424.           function will be placed before the text of  the  a
  425.           or  r  functions.  (The r function is described in
  426.           Section 3.4.)
  427. Note: Within the text put in the output by these  functions,
  428. leading  blanks  and  tabs  will disappear, as always in sed
  429. commands.  To get leading blanks and tabs into  the  output,
  430. precede  the  first desired blank or tab by a backslash; the
  431. backslash will not appear in the output.
  432.  
  433. Example:
  434.  
  435. The list of editing commands:
  436.  
  437.      n
  438.      a\
  439.      XXXX
  440.      d
  441.  
  442. applied to our standard input, produces:
  443.  
  444.      In Xanadu did Kubhla Khan
  445.      XXXX
  446.      Where Alph, the sacred river, ran
  447.      XXXX
  448.      Down to a sunless sea.
  449.  
  450. In this particular case, the same effect would  be  produced
  451. by either of the two following command lists:
  452.  
  453.      n         n
  454.      i\        c\
  455.      XXXX XXXX
  456.      d
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. USD:18-8                SED -- A Non-interactive Text Editor
  467.  
  468.  
  469. 3.2. Substitute Function
  470.  
  471. One  very important function changes parts of lines selected
  472. by a context search within the line.
  473.      (2)s<pattern><replacement><flags> -- substitute  The  s
  474.           function  replaces  part  of  a  line (selected by
  475.           <pattern>) with <replacement>.   It  can  best  be
  476.           read:
  477.                     Substitute  for <pattern>, <replacement>
  478.           The <pattern> argument contains a pattern, exactly
  479.           like  the  patterns  in addresses (see 2.2 above).
  480.           The only difference between <pattern> and  a  con-
  481.           text  address  is that the context address must be
  482.           delimited by slash (`/') characters; <pattern> may
  483.           be  delimited by any character other than space or
  484.           newline.   By  default,  only  the  first   string
  485.           matched  by  <pattern>  is replaced, but see the g
  486.           flag below.   The  <replacement>  argument  begins
  487.           immediately  after the second delimiting character
  488.           of <pattern>, and must be followed immediately  by
  489.           another  instance  of  the  delimiting  character.
  490.           (Thus there are exactly three instances of the de-
  491.           limiting  character.)   The <replacement> is not a
  492.           pattern, and the characters which are  special  in
  493.           patterns  do not have special meaning in <replace-
  494.           ment>.  Instead, other characters are special:
  495.                &    is replaced by  the  string  matched  by
  496.                     <pattern>
  497.                \d (where d is a single digit) is replaced by
  498.                     the dth substring matched  by  parts  of
  499.                     <pattern> enclosed in `\(' and `\)'.  If
  500.                     nested substrings  occur  in  <pattern>,
  501.                     the  dth is determined by counting open-
  502.                     ing delimiters (`\(').  As in  patterns,
  503.                     special  characters  may be made literal
  504.                     by preceding them with backslash  (`\').
  505.           The  <flags>  argument  may  contain the following
  506.           flags:
  507.                g -- substitute <replacement> for  all  (non-
  508.                     overlapping)  instances  of <pattern> in
  509.                     the line.  After a successful  substitu-
  510.                     tion,  the scan for the next instance of
  511.                     <pattern> begins just after the  end  of
  512.                     the  inserted characters; characters put
  513.                     into the line from <replacement> are not
  514.                     rescanned.
  515.                p  -- print the line if a successful replace-
  516.                     ment was done.  The p  flag  causes  the
  517.                     line  to be written to the output if and
  518.                     only if a substitution was actually made
  519.                     by  the s function.  Notice that if sev-
  520.                     eral s functions, each followed by  a  p
  521.                     flag,  successfully  substitute  in  the
  522.                     same input line, multiple copies of  the
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. SED -- A Non-interactive Text Editor                USD:18-9
  533.  
  534.  
  535.                     line  will be written to the output: one
  536.                     for each successful substitution.
  537.                w <filename> -- write the line to a file if a
  538.                     successful  replacement was done.  The w
  539.                     flag causes  lines  which  are  actually
  540.                     substituted  by  the  s  function  to be
  541.                     written to a file named  by  <filename>.
  542.                     If  <filename> exists before sed is run,
  543.                     it is overwritten; if not,  it  is  cre-
  544.                     ated.   A  single  space must separate w
  545.                     and <filename>.   The  possibilities  of
  546.                     multiple,  somewhat  different copies of
  547.                     one input line  being  written  are  the
  548.                     same  as for p.  A maximum of 10 differ-
  549.                     ent file names may be mentioned after  w
  550.                     flags  and w functions (see below), com-
  551.                     bined.
  552.  
  553. Examples:
  554.  
  555. The following command, applied to our standard input,
  556.  
  557.      s/to/by/w changes
  558.  
  559. produces, on the standard output:
  560.  
  561.      In Xanadu did Kubhla Khan
  562.      A stately pleasure dome decree:
  563.      Where Alph, the sacred river, ran
  564.      Through caverns measureless by man
  565.      Down by a sunless sea.
  566.  
  567. and, on the file `changes':
  568.  
  569.      Through caverns measureless by man
  570.      Down by a sunless sea.
  571.  
  572. If the nocopy option is in effect, the command:
  573.  
  574.      s/[.,;?:]/*P&*/gp
  575.  
  576. produces:
  577.  
  578.      A stately pleasure dome decree*P:*
  579.      Where Alph*P,* the sacred river*P,* ran
  580.      Down to a sunless sea*P.*
  581.  
  582. Finally, to illustrate the effect of the g  flag,  the  com-
  583. mand:
  584.  
  585.      /X/s/an/AN/p
  586.  
  587. produces (assuming nocopy mode):
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. USD:18-10               SED -- A Non-interactive Text Editor
  599.  
  600.  
  601. In XANadu did Kubhla Khan
  602.  
  603. and the command:
  604.  
  605.      /X/s/an/AN/gp
  606.  
  607. produces:
  608.  
  609.      In XANadu did Kubhla KhAN
  610.  
  611.  
  612. 3.3. Input-output Functions
  613.  
  614.      (2)p  --  print The print function writes the addressed
  615.           lines to the standard output file.  They are writ-
  616.           ten  at  the  time  the p function is encountered,
  617.           regardless of what succeeding editing commands may
  618.           do to the lines.
  619.      (2)w  <filename> -- write on <filename> The write func-
  620.           tion writes the addressed lines to the file  named
  621.           by <filename>.  If the file previously existed, it
  622.           is overwritten; if not, it is created.  The  lines
  623.           are  written  exactly as they exist when the write
  624.           function is encountered for each line,  regardless
  625.           of  what  subsequent  editing  commands  may do to
  626.           them.  Exactly one space must separate the  w  and
  627.           <filename>.   A maximum of ten different files may
  628.           be mentioned in write functions and w flags  after
  629.           s functions, combined.
  630.      (1)r <filename> -- read the contents of a file The read
  631.           function reads the  contents  of  <filename>,  and
  632.           appends   them  after  the  line  matched  by  the
  633.           address.  The file is read and appended regardless
  634.           of what subsequent editing commands do to the line
  635.           which matched its address.  If r and  a  functions
  636.           are executed on the same line, the text from the a
  637.           functions and the r functions is  written  to  the
  638.           output  in  the  order that the functions are exe-
  639.           cuted.  Exactly one space must separate the r  and
  640.           <filename>.   If  a file mentioned by a r function
  641.           cannot be opened, it is considered  a  null  file,
  642.           not an error, and no diagnostic is given.
  643. NOTE: Since there is a limit to the number of files that can
  644. be opened simultaneously, care should be taken that no  more
  645. than  ten  files  be mentioned in w functions or flags; that
  646. number is reduced by one if any  r  functions  are  present.
  647. (Only one read file is open at one time.)
  648.  
  649. Examples
  650.  
  651. Assume that the file `note1' has the following contents:
  652.  
  653.           Note:   Kubla  Khan  (more  properly  Kublai Khan;
  654.           1216-1294)  was  the  grandson  and  most  eminent
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. SED -- A Non-interactive Text Editor               USD:18-11
  665.  
  666.  
  667.           successor  of  Genghiz (Chingiz) Khan, and founder
  668.           of the Mongol dynasty in China.
  669.  
  670. Then the following command:
  671.  
  672.      /Kubla/r note1
  673.  
  674. produces:
  675.  
  676.      In Xanadu did Kubla Khan
  677.           Note:  Kubla  Khan  (more  properly  Kublai  Khan;
  678.           1216-1294)  was the grandson and most eminent suc-
  679.           cessor of Genghiz (Chingiz) Khan, and  founder  of
  680.           the Mongol dynasty in China.
  681.      A stately pleasure dome decree:
  682.      Where Alph, the sacred river, ran
  683.      Through caverns measureless to man
  684.      Down to a sunless sea.
  685.  
  686.  
  687. 3.4.  Multiple Input-line Functions
  688.  
  689. Three functions, all spelled with capital letters, deal spe-
  690. cially with pattern  spaces  containing  imbedded  newlines;
  691. they  are  intended  principally  to provide pattern matches
  692. across lines in the input.
  693.      (2)N -- Next line The next input line  is  appended  to
  694.           the  current  line  in  the pattern space; the two
  695.           input lines are separated by an imbedded  newline.
  696.           Pattern  matches  may  extend  across the imbedded
  697.           newline(s).
  698.      (2)D -- Delete first part of the pattern  space  Delete
  699.           up to and including the first newline character in
  700.           the current pattern space.  If the  pattern  space
  701.           becomes  empty  (the only newline was the terminal
  702.           newline), read another line from  the  input.   In
  703.           any case, begin the list of editing commands again
  704.           from its beginning.
  705.      (2)P -- Print first part of the pattern space Print  up
  706.           to  and including the first newline in the pattern
  707.           space.
  708. The P and D functions are  equivalent  to  their  lower-case
  709. counterparts  if  there are no imbedded newlines in the pat-
  710. tern space.
  711.  
  712. 3.5.  Hold and Get Functions
  713.  
  714. Four functions save and retrieve part of the input for  pos-
  715. sible later use.
  716.      (2)h  --  hold pattern space The h functions copies the
  717.           contents of the pattern space  into  a  hold  area
  718.           (destroying  the  previous  contents  of  the hold
  719.           area).
  720.      (2)H -- Hold pattern space The H function  appends  the
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. USD:18-12               SED -- A Non-interactive Text Editor
  731.  
  732.  
  733.           contents  of  the pattern space to the contents of
  734.           the hold area; the former  and  new  contents  are
  735.           separated by a newline.
  736.      (2)g -- get contents of hold area The g function copies
  737.           the contents of the hold  area  into  the  pattern
  738.           space  (destroying  the  previous  contents of the
  739.           pattern space).
  740.      (2)G -- Get  contents  of  hold  area  The  G  function
  741.           appends  the contents of the hold area to the con-
  742.           tents of the pattern space;  the  former  and  new
  743.           contents are separated by a newline.
  744.      (2)x  -- exchange The exchange command interchanges the
  745.           contents of the pattern space and the hold area.
  746.  
  747. Example
  748.  
  749. The commands
  750.      1h
  751.      1s/ did.*//
  752.      1x
  753.      G
  754.      s/\n/  :/
  755. applied to our standard example, produce:
  756.      In Xanadu did Kubla Khan  :In Xanadu
  757.      A stately pleasure dome decree:  :In Xanadu
  758.      Where Alph, the sacred river, ran  :In Xanadu
  759.      Through caverns measureless to man  :In Xanadu
  760.      Down to a sunless sea.  :In Xanadu
  761.  
  762. 3.6.  Flow-of-Control Functions
  763.  
  764. These functions do no editing on the input lines,  but  con-
  765. trol  the  application of functions to the lines selected by
  766. the address part.
  767.      (2)! -- Don't The Don't command causes the next command
  768.           (written  on  the same line), to be applied to all
  769.           and only those input lines  not  selected  by  the
  770.           adress part.
  771.      (2){  --  Grouping  The grouping command `{' causes the
  772.           next  set  of  commands  to  be  applied  (or  not
  773.           applied) as a block to the input lines selected by
  774.           the addresses of the grouping command.  The  first
  775.           of  the commands under control of the grouping may
  776.           appear on the same line as the `{' or on the  next
  777.           line.
  778.  
  779. The group of commands is terminated by a matching `}' stand-
  780. ing on a line by itself.
  781.  
  782. Groups can be nested.
  783. (0):<label> -- place a label  The  label  function  marks  a
  784. place  in the list of editing commands which may be referred
  785. to by b and t functions.  The <label> may be any sequence of
  786. eight  or fewer characters; if two different colon functions
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. SED -- A Non-interactive Text Editor               USD:18-13
  797.  
  798.  
  799. have identical labels, a compile  time  diagnostic  will  be
  800. generated, and no execution attempted.
  801. (2)b<label>  --  branch  to label The branch function causes
  802. the sequence of editing commands being applied to  the  cur-
  803. rent  input line to be restarted immediately after the place
  804. where a colon function with the  same  <label>  was  encoun-
  805. tered.   If  no  colon  function  with the same label can be
  806. found after all the editing commands have been  compiled,  a
  807. compile  time  diagnostic  is  produced, and no execution is
  808. attempted.  A b function with no <label> is taken  to  be  a
  809. branch  to the end of the list of editing commands; whatever
  810. should be done with the current  input  line  is  done,  and
  811. another  input line is read; the list of editing commands is
  812. restarted from the beginning on the new line.
  813. (2)t<label> --  test  substitutions  The  t  function  tests
  814. whether  any  successful substitutions have been made on the
  815. current input line; if so, it branches to <label>;  if  not,
  816. it does nothing.  The flag which indicates that a successful
  817. substitution has been executed is reset by:
  818.           1) reading a new input line, or
  819.           2) executing a t function.
  820.  
  821. 3.7. Miscellaneous Functions
  822.  
  823.      (1)= -- equals The = function writes  to  the  standard
  824.           output  the line number of the line matched by its
  825.           address.
  826.      (1)q -- quit The q function causes the current line  to
  827.           be  written  to  the output (if it should be), any
  828.           appended or read text to be written, and execution
  829.           to be terminated.
  830.  
  831.  
  832. Reference
  833.  
  834. [1]  Ken  Thompson  and Dennis M. Ritchie, The UNIX Program-
  835.      mer's Manual.  Bell Laboratories, 1978.
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.