home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / gcc263-utildoc.lha / gnu / man / man1 / sed.1 < prev    next >
Encoding:
Text File  |  1994-12-19  |  11.1 KB  |  228 lines

  1.  
  2.  
  3.  sed(C)                        06 January 1993                         sed(C)
  4.  
  5.  _N_a_m_e
  6.  
  7.     sed - invoke the stream editor
  8.  
  9.  _S_y_n_t_a_x
  10.  
  11.     sseedd [ --nn ] [ --ee _ss_cc_rr_ii_pp_tt ] [ --ff _ss_ff_ii_ll_ee ] [ _ff_ii_ll_ee_ss ]
  12.  
  13.  _D_e_s_c_r_i_p_t_i_o_n
  14.  
  15.     The sseedd command copies the named _ff_ii_ll_ee_ss (standard input default) to the
  16.     standard output, edited according to a script of commands.  The --ee option
  17.     causes the script to be read literally from the next argument, which is
  18.     usually quoted to protect it from the shell.  The --ff option causes the
  19.     script to be taken from file _ss_ff_ii_ll_ee; these options accumulate. If there is
  20.     just one --ee option and no --ff options, the flag --ee may be omitted. The --nn
  21.     option suppresses the default output.  A script consists of editing com-
  22.     mands, one per line, of the following form:
  23.  
  24.        [ _aa_dd_dd_rr_ee_ss_ss [ , _aa_dd_dd_rr_ee_ss_ss ] ] _ff_uu_nn_cc_tt_ii_oo_nn [ _aa_rr_gg_uu_mm_ee_nn_tt_ss ]
  25.  
  26.     In normal operation, sseedd cyclically copies a line of input into a pattern
  27.     space (unless there is something left after a DD command), applies in
  28.     sequence all commands whose addresses select that pattern space, and at
  29.     the end of the script copies the pattern space to the standard output
  30.     (except under --nn) and deletes the pattern space.
  31.  
  32.     A semicolon (;) can be used as a command delimiter.
  33.  
  34.     Some of the commands use a hold space to save all or part of the pattern
  35.     space for subsequent retrieval.
  36.  
  37.     An address is either a decimal number that counts input lines cumula-
  38.     tively across files, a ``$'' that addresses the last line of input, or a
  39.     context address, that is, a _//_rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn_// in the style of eedd(C)
  40.     modified as follows:
  41.  
  42.     +  In a context address, the construction _\\_??_rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn_??, where
  43.        ``?'' is any character, is identical to _//_rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn_//.  Note
  44.        that in the context address _\\_xx_aa_bb_cc_\\_xx_dd_ee_ff_xx, the second _xx stands for
  45.        itself, so that the standard expression is _aa_bb_cc_xx_dd_ee_ff.
  46.  
  47.     +  The escape sequence \n matches a newline embedded in the pattern
  48.        space.
  49.  
  50.     +  A dot (.) matches any character except the terminal newline of the
  51.        pattern space.
  52.  
  53.     +  A command line with no addresses selects every pattern space.
  54.  
  55.     +  A command line with one address selects each pattern space that
  56.        matches the address.
  57.  
  58.     +  A command line with two addresses separated by a comma selects the
  59.        inclusive range from the first pattern space that matches the first
  60.        address through the next pattern space that matches the second.  (If
  61.        the second address is a number less than or equal to the line number
  62.        first selected, only one line is selected.) Thereafter, the process is
  63.        repeated, looking again for the first address.
  64.  
  65.     Editing commands can be applied only to nonselected pattern spaces by use
  66.     of the negation function ``!'' (below).
  67.  
  68.     In the following list of functions, the maximum number of permissible
  69.     addresses for each function is indicated in parentheses.
  70.  
  71.     The _tt_ee_xx_tt argument consists of one or more lines, all but the last of
  72.     which end with backslashes to hide the newlines.  Backslashes in text are
  73.     treated like backslashes in the replacement string of an ss command, and
  74.     may be used to protect initial blanks and tabs against the stripping that
  75.     is done on every script line.  The _rr_ff_ii_ll_ee or _ww_ff_ii_ll_ee argument must terminate
  76.     the command line and must be preceded by one blank. Each _ww_ff_ii_ll_ee is created
  77.     before processing begins. There can be at most 10 distinct _ww_ff_ii_ll_ee argu-
  78.     ments.
  79.  
  80.     (1) aa\\ _tt_ee_xx_tt
  81.               Appends _tt_ee_xx_tt, placing it on the output before reading the next
  82.               input line.
  83.  
  84.     (2) bb _ll_aa_bb_ee_ll
  85.               Branches to the :: command bearing the _ll_aa_bb_ee_ll.  If _ll_aa_bb_ee_ll is
  86.               empty, branches to the end of the script.
  87.  
  88.     (2) cc\\ _tt_ee_xx_tt
  89.               Changes text by deleting the pattern space and then appending
  90.               _tt_ee_xx_tt.  With 0 or 1 address or at the end of a 2-address range,
  91.               places _tt_ee_xx_tt on the output and starts the next cycle.
  92.  
  93.     (2) dd     Deletes the pattern space and starts the next cycle.
  94.  
  95.     (2) DD     Deletes the initial segment of the pattern space through the
  96.               first newline and starts the next cycle.
  97.  
  98.     (2) gg     Replaces the contents of the pattern space with the contents of
  99.               the hold space.
  100.  
  101.     (2) GG     Appends the contents of the hold space to the pattern space.
  102.  
  103.     (2) hh     Replaces the contents of the hold space with the contents of
  104.               the pattern space.
  105.  
  106.     (2) HH     Appends the contents of the pattern space to the hold space.
  107.  
  108.     (1) ii\\ _tt_ee_xx_tt
  109.               Insert.  Places _tt_ee_xx_tt on the standard output.
  110.  
  111.     (2) ll     Lists the pattern space on the standard output with nonprinting
  112.               characters spelled in two-digit ASCII and long lines folded.
  113.  
  114.     (2) nn     Copies the pattern space to the standard output.  Replaces the
  115.               pattern space with the next line of input.
  116.  
  117.     (2) NN     Appends the next line of input to the pattern space with an
  118.               embedded newline.  (The current line number changes.)
  119.  
  120.     (2) pp     Prints (copies) the pattern space on the standard output.
  121.  
  122.     (2) PP     Prints (copies) the initial segment of the pattern space
  123.               through the first newline to the standard output.
  124.  
  125.     (1) qq     Quits sseedd by branching to the end of the script.  No new cycle
  126.               is started.
  127.  
  128.     (2) rr _rr_ff_ii_ll_ee
  129.               Reads the contents of _rr_ff_ii_ll_ee and places them on the output
  130.               before reading the next input line.
  131.  
  132.     (2) ss _//_rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn_//_rr_ee_pp_ll_aa_cc_ee_mm_ee_nn_tt_//_ff_ll_aa_gg_ss
  133.               Substitutes the _rr_ee_pp_ll_aa_cc_ee_mm_ee_nn_tt string for instances of the _rr_ee_gg_uu_ll_aa_rr
  134.               _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn in the pattern space.  Any character may be used
  135.               instead of ``/''.  For a more detailed description, see eedd(C).
  136.               _FF_ll_aa_gg_ss is zero or more of:
  137.  
  138.               _nn    _nn=1-512.  Substitute for just the _nnth occurrence of the
  139.                    _rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn.
  140.  
  141.               gg    Globally substitutes for all non-overlapping instances of
  142.                    the _rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn rather than just the first one.
  143.  
  144.               pp    Prints the pattern space if a replacement was made.
  145.  
  146.               ww _ww_ff_ii_ll_ee
  147.                    Writes the pattern space to _ww_ff_ii_ll_ee if a replacement was
  148.                    made.
  149.  
  150.     (2) tt _ll_aa_bb_ee_ll
  151.               Branches to the colon (:) command bearing _ll_aa_bb_ee_ll if any substi-
  152.               tutions have been made since the most recent reading of an
  153.               input line or execution of a tt command.  If _ll_aa_bb_ee_ll is empty, tt
  154.               branches to the end of the script.
  155.  
  156.     (2) ww _ww_ff_ii_ll_ee
  157.               Writes the pattern space to _ww_ff_ii_ll_ee.
  158.  
  159.     (2) xx     Exchanges the contents of the pattern and hold spaces.
  160.  
  161.     (2) yy _//_ss_tt_rr_ii_nn_gg_11_//_ss_tt_rr_ii_nn_gg_22_//
  162.               Replaces all occurrences of characters in _ss_tt_rr_ii_nn_gg_11 with the cor-
  163.               responding characters in _ss_tt_rr_ii_nn_gg_22.  The lengths of _ss_tt_rr_ii_nn_gg_11 and
  164.               _ss_tt_rr_ii_nn_gg_22 must be equal.
  165.  
  166.     (2) !! _ff_uu_nn_cc_tt_ii_oo_nn
  167.               Applies the _ff_uu_nn_cc_tt_ii_oo_nn (or group, if _ff_uu_nn_cc_tt_ii_oo_nn is ``{'') only to
  168.               lines NOT selected by the address(es).
  169.  
  170.     (0) :: _ll_aa_bb_ee_ll
  171.               This command does nothing; it bears a _ll_aa_bb_ee_ll for bb and tt com-
  172.               mands to branch to.
  173.  
  174.     (1) ==     Places the current line number on the standard output as a
  175.               line.
  176.  
  177.     (2) {{     Executes the following commands through a matching ``}'' only
  178.               when the pattern space is selected.
  179.     (0)       An empty command is ignored.
  180.  
  181.  _E_x_a_m_p_l_e_s
  182.  
  183.     The following examples assume the use of sshh or kksshh.
  184.  
  185.     The most common use of sseedd is to edit a file from within a shell script.
  186.     In this example, every occurrence of the string ``sysman'' in the file
  187.     _i_n_f_i_l_e is replaced by ``System Manager''. A temporary file TTMMPP is used to
  188.     hold the intermediate result of the edit:
  189.  
  190.        TMP=/usr/tmp/tmpfile_$$
  191.        sed  -e  's/sysman/System Manager/g'  <  infile  >  $TMP
  192.        mv  $TMP  infile
  193.  
  194.     sseedd can be used to strip all lines from a file which do not contain a
  195.     certain string. In this example, all lines in the file _i_n_f_i_l_e which start
  196.     with a hash ``#'' are echoed to the screen:
  197.  
  198.        sed -e '/^#/!d' < infile
  199.  
  200.     If several editing commands must be carried out on a file, but the param-
  201.     eters for the edit are to be supplied by the user, then a document can be
  202.     used to build a temporary edit script for sseedd to use. The following exam-
  203.     ple removes all occurrences of the strings given as arguments to the
  204.     script from the file _i_n_f_i_l_e. The name of the temporary script is held by
  205.     the variable SSCCRRIIPPTT:
  206.  
  207.        SCRIPT=/usr/tmp/script_$$
  208.        for name in $*
  209.        do
  210.                cat >> $SCRIPT <<!
  211.                        s/${name}//g
  212.                !
  213.        done
  214.        TMPFILE=/usr/tmp/tmpfile_$$
  215.        sed  -f  $SCRIPT  <  infile  >  $TMPFILE
  216.        mv  $TMPFILE  infile
  217.        rm  $SCRIPT
  218.  
  219.     Another use of sseedd is to process the output from other commands. Here the
  220.     ppss command is filtered using sseedd to report the status of all processes
  221.     other than those owned by the super user:
  222.  
  223.        ppss --eeff || sseedd --ee ''//^^[[<<SSppaaccee>><<TTaabb>>]]**rroooott//dd''
  224.  
  225.  _S_e_e _a_l_s_o
  226.  
  227.     aawwkk(C), eedd(C) and ggrreepp(C).
  228.