home *** CD-ROM | disk | FTP | other *** search
-
-
- sed(C) 06 January 1993 sed(C)
-
- _N_a_m_e
-
- sed - invoke the stream editor
-
- _S_y_n_t_a_x
-
- sseedd [ --nn ] [ --ee _ss_cc_rr_ii_pp_tt ] [ --ff _ss_ff_ii_ll_ee ] [ _ff_ii_ll_ee_ss ]
-
- _D_e_s_c_r_i_p_t_i_o_n
-
- The sseedd command copies the named _ff_ii_ll_ee_ss (standard input default) to the
- standard output, edited according to a script of commands. The --ee option
- causes the script to be read literally from the next argument, which is
- usually quoted to protect it from the shell. The --ff option causes the
- script to be taken from file _ss_ff_ii_ll_ee; these options accumulate. If there is
- just one --ee option and no --ff options, the flag --ee may be omitted. The --nn
- option suppresses the default output. A script consists of editing com-
- mands, one per line, of the following form:
-
- [ _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 ]
-
- In normal operation, sseedd cyclically copies a line of input into a pattern
- space (unless there is something left after a DD command), applies in
- sequence all commands whose addresses select that pattern space, and at
- the end of the script copies the pattern space to the standard output
- (except under --nn) and deletes the pattern space.
-
- A semicolon (;) can be used as a command delimiter.
-
- Some of the commands use a hold space to save all or part of the pattern
- space for subsequent retrieval.
-
- An address is either a decimal number that counts input lines cumula-
- tively across files, a ``$'' that addresses the last line of input, or a
- 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)
- modified as follows:
-
- + In a context address, the construction _\\_??_rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn_??, where
- ``?'' is any character, is identical to _//_rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn_//. Note
- that in the context address _\\_xx_aa_bb_cc_\\_xx_dd_ee_ff_xx, the second _xx stands for
- itself, so that the standard expression is _aa_bb_cc_xx_dd_ee_ff.
-
- + The escape sequence \n matches a newline embedded in the pattern
- space.
-
- + A dot (.) matches any character except the terminal newline of the
- pattern space.
-
- + A command line with no addresses selects every pattern space.
-
- + A command line with one address selects each pattern space that
- matches the address.
-
- + A command line with two addresses separated by a comma selects the
- inclusive range from the first pattern space that matches the first
- address through the next pattern space that matches the second. (If
- the second address is a number less than or equal to the line number
- first selected, only one line is selected.) Thereafter, the process is
- repeated, looking again for the first address.
-
- Editing commands can be applied only to nonselected pattern spaces by use
- of the negation function ``!'' (below).
-
- In the following list of functions, the maximum number of permissible
- addresses for each function is indicated in parentheses.
-
- The _tt_ee_xx_tt argument consists of one or more lines, all but the last of
- which end with backslashes to hide the newlines. Backslashes in text are
- treated like backslashes in the replacement string of an ss command, and
- may be used to protect initial blanks and tabs against the stripping that
- is done on every script line. The _rr_ff_ii_ll_ee or _ww_ff_ii_ll_ee argument must terminate
- the command line and must be preceded by one blank. Each _ww_ff_ii_ll_ee is created
- before processing begins. There can be at most 10 distinct _ww_ff_ii_ll_ee argu-
- ments.
-
- (1) aa\\ _tt_ee_xx_tt
- Appends _tt_ee_xx_tt, placing it on the output before reading the next
- input line.
-
- (2) bb _ll_aa_bb_ee_ll
- Branches to the :: command bearing the _ll_aa_bb_ee_ll. If _ll_aa_bb_ee_ll is
- empty, branches to the end of the script.
-
- (2) cc\\ _tt_ee_xx_tt
- Changes text by deleting the pattern space and then appending
- _tt_ee_xx_tt. With 0 or 1 address or at the end of a 2-address range,
- places _tt_ee_xx_tt on the output and starts the next cycle.
-
- (2) dd Deletes the pattern space and starts the next cycle.
-
- (2) DD Deletes the initial segment of the pattern space through the
- first newline and starts the next cycle.
-
- (2) gg Replaces the contents of the pattern space with the contents of
- the hold space.
-
- (2) GG Appends the contents of the hold space to the pattern space.
-
- (2) hh Replaces the contents of the hold space with the contents of
- the pattern space.
-
- (2) HH Appends the contents of the pattern space to the hold space.
-
- (1) ii\\ _tt_ee_xx_tt
- Insert. Places _tt_ee_xx_tt on the standard output.
-
- (2) ll Lists the pattern space on the standard output with nonprinting
- characters spelled in two-digit ASCII and long lines folded.
-
- (2) nn Copies the pattern space to the standard output. Replaces the
- pattern space with the next line of input.
-
- (2) NN Appends the next line of input to the pattern space with an
- embedded newline. (The current line number changes.)
-
- (2) pp Prints (copies) the pattern space on the standard output.
-
- (2) PP Prints (copies) the initial segment of the pattern space
- through the first newline to the standard output.
-
- (1) qq Quits sseedd by branching to the end of the script. No new cycle
- is started.
-
- (2) rr _rr_ff_ii_ll_ee
- Reads the contents of _rr_ff_ii_ll_ee and places them on the output
- before reading the next input line.
-
- (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
- 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
- _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn in the pattern space. Any character may be used
- instead of ``/''. For a more detailed description, see eedd(C).
- _FF_ll_aa_gg_ss is zero or more of:
-
- _nn _nn=1-512. Substitute for just the _nnth occurrence of the
- _rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn.
-
- gg Globally substitutes for all non-overlapping instances of
- the _rr_ee_gg_uu_ll_aa_rr _ee_xx_pp_rr_ee_ss_ss_ii_oo_nn rather than just the first one.
-
- pp Prints the pattern space if a replacement was made.
-
- ww _ww_ff_ii_ll_ee
- Writes the pattern space to _ww_ff_ii_ll_ee if a replacement was
- made.
-
- (2) tt _ll_aa_bb_ee_ll
- Branches to the colon (:) command bearing _ll_aa_bb_ee_ll if any substi-
- tutions have been made since the most recent reading of an
- input line or execution of a tt command. If _ll_aa_bb_ee_ll is empty, tt
- branches to the end of the script.
-
- (2) ww _ww_ff_ii_ll_ee
- Writes the pattern space to _ww_ff_ii_ll_ee.
-
- (2) xx Exchanges the contents of the pattern and hold spaces.
-
- (2) yy _//_ss_tt_rr_ii_nn_gg_11_//_ss_tt_rr_ii_nn_gg_22_//
- Replaces all occurrences of characters in _ss_tt_rr_ii_nn_gg_11 with the cor-
- responding characters in _ss_tt_rr_ii_nn_gg_22. The lengths of _ss_tt_rr_ii_nn_gg_11 and
- _ss_tt_rr_ii_nn_gg_22 must be equal.
-
- (2) !! _ff_uu_nn_cc_tt_ii_oo_nn
- Applies the _ff_uu_nn_cc_tt_ii_oo_nn (or group, if _ff_uu_nn_cc_tt_ii_oo_nn is ``{'') only to
- lines NOT selected by the address(es).
-
- (0) :: _ll_aa_bb_ee_ll
- This command does nothing; it bears a _ll_aa_bb_ee_ll for bb and tt com-
- mands to branch to.
-
- (1) == Places the current line number on the standard output as a
- line.
-
- (2) {{ Executes the following commands through a matching ``}'' only
- when the pattern space is selected.
- (0) An empty command is ignored.
-
- _E_x_a_m_p_l_e_s
-
- The following examples assume the use of sshh or kksshh.
-
- The most common use of sseedd is to edit a file from within a shell script.
- In this example, every occurrence of the string ``sysman'' in the file
- _i_n_f_i_l_e is replaced by ``System Manager''. A temporary file TTMMPP is used to
- hold the intermediate result of the edit:
-
- TMP=/usr/tmp/tmpfile_$$
- sed -e 's/sysman/System Manager/g' < infile > $TMP
- mv $TMP infile
-
- sseedd can be used to strip all lines from a file which do not contain a
- certain string. In this example, all lines in the file _i_n_f_i_l_e which start
- with a hash ``#'' are echoed to the screen:
-
- sed -e '/^#/!d' < infile
-
- If several editing commands must be carried out on a file, but the param-
- eters for the edit are to be supplied by the user, then a document can be
- used to build a temporary edit script for sseedd to use. The following exam-
- ple removes all occurrences of the strings given as arguments to the
- script from the file _i_n_f_i_l_e. The name of the temporary script is held by
- the variable SSCCRRIIPPTT:
-
- SCRIPT=/usr/tmp/script_$$
- for name in $*
- do
- cat >> $SCRIPT <<!
- s/${name}//g
- !
- done
- TMPFILE=/usr/tmp/tmpfile_$$
- sed -f $SCRIPT < infile > $TMPFILE
- mv $TMPFILE infile
- rm $SCRIPT
-
- Another use of sseedd is to process the output from other commands. Here the
- ppss command is filtered using sseedd to report the status of all processes
- other than those owned by the super user:
-
- ppss --eeff || sseedd --ee ''//^^[[<<SSppaaccee>><<TTaabb>>]]**rroooott//dd''
-
- _S_e_e _a_l_s_o
-
- aawwkk(C), eedd(C) and ggrreepp(C).
-