sed: Regexp Addresses

1 
1 4.3 selecting lines by text matching
1 ====================================
1 
1 GNU 'sed' supports the following regular expression addresses.  The
11 default regular expression is ⇒Basic Regular Expression (BRE) BRE
 syntax.  If '-E' or '-r' options are used, The regular expression should
1 be in ⇒Extended Regular Expression (ERE) ERE syntax. syntax.
1 ⇒BRE vs ERE.
1 
1 '/REGEXP/'
1      This will select any line which matches the regular expression
1      REGEXP.  If REGEXP itself includes any '/' characters, each must be
1      escaped by a backslash ('\').
1 
1      The following command prints lines in '/etc/passwd' which end with
1      'bash'(1):
1 
1           sed -n '/bash$/p' /etc/passwd
1 
1      The empty regular expression '//' repeats the last regular
1      expression match (the same holds if the empty regular expression is
1      passed to the 's' command).  Note that modifiers to regular
1      expressions are evaluated when the regular expression is compiled,
1      thus it is invalid to specify them together with the empty regular
1      expression.
1 
1 '\%REGEXP%'
1      (The '%' may be replaced by any other single character.)
1 
1      This also matches the regular expression REGEXP, but allows one to
1      use a different delimiter than '/'.  This is particularly useful if
1      the REGEXP itself contains a lot of slashes, since it avoids the
1      tedious escaping of every '/'.  If REGEXP itself includes any
1      delimiter characters, each must be escaped by a backslash ('\').
1 
1      The following two commands are equivalent.  They print lines which
1      start with '/home/alice/documents/':
1 
1           sed -n '/^\/home\/alice\/documents\//p'
1           sed -n '\%^/home/alice/documents/%p'
1           sed -n '\;^/home/alice/documents/;p'
1 
1 '/REGEXP/I'
1 '\%REGEXP%I'
1      The 'I' modifier to regular-expression matching is a GNU extension
1      which causes the REGEXP to be matched in a case-insensitive manner.
1 
1      In many other programming languages, a lower case 'i' is used for
1      case-insensitive regular expression matching.  However, in 'sed'
1      the 'i' is used for the insert command (⇒insert command).
1 
1      Observe the difference between the following examples.
1 
1      In this example, '/b/I' is the address: regular expression with 'I'
1      modifier.  'd' is the delete command:
1 
1           $ printf "%s\n" a b c | sed '/b/Id'
1           a
1           c
1 
1      Here, '/b/' is the address: a regular expression.  'i' is the
1      insert command.  'd' is the value to insert.  A line with 'd' is
1      then inserted above the matched line:
1 
1           $ printf "%s\n" a b c | sed '/b/id'
1           a
1           d
1           b
1           c
1 
1 '/REGEXP/M'
1 '\%REGEXP%M'
1      The 'M' modifier to regular-expression matching is a GNU 'sed'
1      extension which directs GNU 'sed' to match the regular expression
1      in 'multi-line' mode.  The modifier causes '^' and '$' to match
1      respectively (in addition to the normal behavior) the empty string
1      after a newline, and the empty string before a newline.  There are
1      special character sequences ('\`' and '\'') which always match the
1      beginning or the end of the buffer.  In addition, the period
1      character does not match a new-line character in multi-line mode.
1 
1    Regex addresses operate on the content of the current pattern space.
1 If the pattern space is changed (for example with 's///' command) the
1 regular expression matching will operate on the changed text.
1 
1    In the following example, automatic printing is disabled with '-n'.
1 The 's/2/X/' command changes lines containing '2' to 'X'.  The command
1 '/[0-9]/p' matches lines with digits and prints them.  Because the
1 second line is changed before the '/[0-9]/' regex, it will not match and
1 will not be printed:
1 
1      $ seq 3 | sed -n 's/2/X/ ; /[0-9]/p'
1      1
1      3
1 
1    ---------- Footnotes ----------
1 
1    (1) There are of course many other ways to do the same, e.g.
1      grep 'bash$' /etc/passwd
1      awk -F: '$7 == "/bin/bash"' /etc/passwd
1