sed.txt

来自「linux下的命令」· 文本 代码 · 共 1,789 行 · 第 1/4 页

TXT
1,789
字号
     in the input.



     If a  command has  one address, it  is applied to  all lines

     which match that address.



     If a command  has two addresses, it is  applied to the first

     line which matches the  first address, and to all subsequent

     lines until (and including)  the first subsequent line which

     matches  the second  address.  Then  an attempt  is  made on

     subsequent lines  to again match the first  address, and the

     process is repeated.



     Two addresses are separated by a comma.



Examples:



          /an/      matches lines 1, 3, 4 in our sample text

          /an.*an/  matches line 1

          /^an/     matches no lines

          /./       matches all lines

          /\./      matches line 5

          /r*an/    matches lines 1,3, 4 (number = zero!)

          /\(an\).*\1/        matches line 1



3. FUNCTIONS



     All  functions are  named  by a  single  character.  In  the

     following summary, the maximum number of allowable addresses

     is given enclosed in  parentheses, then the single character

     function name, possible arguments  enclosed in angles (< >),

     an  expanded  English  translation of  the  single-character

     name, and finally a  description of what each function does.

     The  angles  around  the  arguments  are  not  part  of  the

     argument,  and  should  not   be  typed  in  actual  editing

     commands.



3.1. Whole-line Oriented Functions



          (2)d --  delete lines The  d function deletes  from the

               file  (does not write   to the  output) all  those

               lines  matched  by   its  address(es).   It   also

               has  the  side effect  that  no  further  commands

               are attempted   on the  corpse of a  deleted line;

               as soon as the d function is executed, a  new line

               is   read  from  the  input,   and  the  list   of

               editing    commands   is   re-started   from   the

               beginning on the new line.

          (2)n --  next line The  n function reads the  next line

               from  the  input,   replacing  the  current  line.

               The current  line is written to  the  output if it

               should  be.   The  list  of  editing  commands  is

               continued following the n command.

          (1)a\

          <text> -- append lines

               The a  function causes  the argument <text>  to be

               written to  the output  after the line  matched by

               its   address.   The   a  command   is  inherently

               multi-line; a  must appear at  the end of  a line,

               and <text>  may contain  any number of  lines.  To

               preserve  the  one-command-to-a-line fiction,  the

               interior  newlines must be  hidden by  a backslash

               character (`\') immediately preceding the newline.

               The  <text> argument  is terminated  by  the first

               unhidden  newline (the  first one  not immediately

               preceded  by backslash).   Once an  a  function is

               successfully executed,  <text> will be  written to

               the output regardless of what later commands do to

               the line which  triggered it.  The triggering line

               may  be  deleted entirely;  <text>  will still  be

               written to the output.   The <text> is not scanned

               for address  matches, and no  editing commands are

               attempted on it.  It  does not cause any change in

               the line-number counter.

          (1)i\

          <text> -- insert lines

               The  i  function   behaves  identically to  the  a

               function,  except that  <text> is  written  to the

               output  before   the  matched  line.    All  other

               comments  about  the a  function  apply  to the  i

               function as well.

          (2)c\

          <text> -- change lines

               The c  function deletes the lines  selected by its

               address(es), and  replaces them with  the lines in

               <text>.  Like  a and  i, c must  be followed  by a

               newline  hidden by a  backslash; and  interior new

               lines  in <text>  must be  hidden  by backslashes.

               The  c   command  may  have   two  addresses,  and

               therefore select  a range  of lines.  If  it does,

               all the  lines in the range are  deleted, but only

               one copy  of <text> is written to  the output, not

               one  copy per  line  deleted.  As  with  a and  i,

               <text> is not scanned  for address matches, and no

               editing commands are attempted on it.  It does not

               change the  line-number counter.  After a line has

               been deleted by a  c function, no further commands

               are attempted on the  corpse.  If text is appended

               after a line by a  or r functions, and the line is

               subsequently changed,  the text inserted  by the c

               function will  be placed before the text  of the a

               or r  functions.  (The r function  is described in

               Section 3.4.)

     Note: Within the text put  in the output by these functions,

     leading  blanks and tabs  will disappear,  as always  in sed

     commands.  To  get leading blanks and tabs  into the output,

     precede the first  desired blank or tab by  a backslash; the

     backslash will not appear in the output.



Example:



     The list of editing commands:



          n

          a\

          XXXX

          d



     applied to our standard input, produces:



          In Xanadu did Kubhla Khan

          XXXX

          Where Alph, the sacred river, ran

          XXXX

          Down to a sunless sea.



     In this  particular case, the same effect  would be produced

     by either of the two following command lists:



          n                   n

          i\                  c\

          XXXX      XXXX

          d



3.2. Substitute Function



     One very important function  changes parts of lines selected

     by a context search within the line.

          (2)s<pattern><replacement><flags>  -- substitute  The s

               function     replaces     part    of     a    line

               (selected   by <pattern>) with  <replacement>.  It

               can best be read:

                         Substitute  for <pattern>, <replacement>

               The <pattern> argument contains a pattern, exactly

               like  the patterns in  addresses (see  2.2 above).

               The  only  difference   between  <pattern>  and  a

               context address  is that the  context address must

               be delimited by  slash (`/') characters; <pattern>

               may be delimited by any character other than space

               or  newline.  By  default, only  the  first string

               matched by  <pattern> is  replaced, but see  the g

               flag  below.   The  <replacement> argument  begins

               immediately after  the second delimiting character

               of <pattern>, and  must be followed immediately by

               another  instance  of  the  delimiting  character.

               (Thus  there are  exactly three  instances  of the

               delimiting character.)  The <replacement> is not a

               pattern, and  the characters which  are special in

               patterns   do   not   have  special   meaning   in

               <replacement>.   Instead,   other  characters  are

               special:

                    &         is  replaced by the  string matched

                         by <pattern>

                    \d (where d is a single digit) is replaced by

                         the    dth     substring   matched    by

                         parts  of  <pattern>  enclosed  in  `\('

                         and  `\)'.    If nested substrings occur

                         in <pattern>, the  dth is determined  by

                         counting opening delimiters (`\(').   As

                         in patterns,  special characters may  be

                         made  literal  by  preceding   them with

                         backslash (`\').

               The  <flags> argument  may  contain the  following

               flags:

                    g   --  substitute   <replacement>   for  all

                         (non-overlapping)       instances     of

                         <pattern>   in  the    line.    After  a

                         successful  substitution, the  scan  for

                         the  next  instance of  <pattern> begins

                         just  after  the  end  of   the inserted

                         characters; characters put into the line

                         from <replacement> are not rescanned.

                    p   --  print  the   line  if   a  successful

                         replacement  was   done.    The  p  flag

                         causes  the line to  be written   to the

                         output if and only if a substitution was

                         actually   made  by   the   s  function.

                         Notice     that     if     several     s

                         functions,   each   followed  by   a   p

                         flag, successfully   substitute  in  the

                         same   input line,  multiple  copies  of

                         the   line  will  be  written   to   the

                         output:    one   for    each  successful

                         substitution.

                    w <filename> -- write the line to a file if a

                         successful replacement was  done.  The w

                         flag  causes  lines which  are  actually

                         substituted  by  the  s function   to be

                         written to  a file named  by <filename>.

                         If  <filename> exists before sed is run,

                         it  is   overwritten;  if  not,   it  is

                         created.   A  single space must separate

                         w       and        <filename>.       The

                         possibilities   of  multiple,   somewhat

                         different   copies   of  one input  line

                         being written  are the same as for p.  A

                         maximum of  10  different file names may

                         be   mentioned  after  w  flags  and   w

                         functions (see below), combined.



Examples:



     The following command, applied to our standard input,



          s/to/by/w changes



     produces, on the standard output:



          In Xanadu did Kubhla Khan

          A stately pleasure dome decree:

          Where Alph, the sacred river, ran

          Through caverns measureless by man

          Down by a sunless sea.



     and, on the file `changes':

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?