Skip to content

Instantly share code, notes, and snippets.

@epcim
Last active February 1, 2024 08:40
Show Gist options
  • Save epcim/9882c828b39b3d361eb9ccc665cc0cff to your computer and use it in GitHub Desktop.
Save epcim/9882c828b39b3d361eb9ccc665cc0cff to your computer and use it in GitHub Desktop.

Revisions

  1. epcim revised this gist Jul 27, 2017. 1 changed file with 82 additions and 0 deletions.
    82 changes: 82 additions & 0 deletions sed-mutli-line-replacement-between-two.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    # doc
    http://fahdshariff.blogspot.cz/2012/12/sed-mutli-line-replacement-between-two.html

    Sed: Mutli-Line Replacement Between Two Patterns
    This post has some useful sed commands which can be used to perform replacements and deletes between two patterns across multiple lines. For example, consider the following file:
    $ cat file
    line 1
    line 2
    foo
    line 3
    line 4
    line 5
    bar
    line 6
    line 7
    1) Replace text on each line between two patterns (inclusive):
    To perform a replacement on each line between foo and bar, including the lines containing foo and bar, use the following:
    $ sed '/foo/,/bar/{s/./x/g}' file
    line 1
    line 2
    xxx
    xxxxxx
    xxxxxx
    xxxxxx
    xxx
    line 6
    line 7
    2) Replace text on each line between two patterns (exclusive):
    To perform a replacement on each line between foo and bar, excluding the lines containing foo and bar, use the following:
    $ sed '/foo/,/bar/{/foo/n;/bar/!{s/./x/g}}' file
    line 1
    line 2
    foo
    xxxxxx
    xxxxxx
    xxxxxx
    bar
    line 6
    line 7
    3) Delete lines between two patterns (inclusive):
    To delete all lines between foo and bar, including the lines containing foo and bar, use the same replacement sed command as shown above, but simply change the replacement expression to a delete.
    $ sed '/foo/,/bar/d' file
    line 1
    line 2
    line 6
    line 7
    4) Delete lines between two patterns (exclusive):
    To delete all lines between foo and bar, excluding the lines containing foo and bar, use the same replacement sed command as shown above, but simply change the replacement expression to a delete.
    $ sed '/foo/,/bar/ {/foo/n;/bar/!d}' file
    line 1
    line 2
    foo
    bar
    line 6
    line 7
    5) Replace all lines between two patterns (inclusive):
    To perform a replacement on a block of lines between foo and bar, including the lines containing foo and bar, use:
    $ sed -n '/foo/{:a;N;/bar/!ba;N;s/.*\n/REPLACEMENT\n/};p' file
    line 1
    line 2
    REPLACEMENT
    line 6
    line 7
    How it works:
    /foo/{ # when "foo" is found
    :a # create a label "a"
    N # store the next line
    /bar/!ba # goto "a" and keep looping and storing lines until "bar" is found
    N # store the line containing "bar"
    s/.*\n/REPLACEMENT\n/ # delete the lines
    }
    p # print
    6) Replace all lines between two patterns (exclusive):
    To perform a replacement on a block of lines between foo and bar, excluding the lines containing foo and bar, use:
    $ sed -n '/foo/{p;:a;N;/bar/!ba;s/.*\n/REPLACEMENT\n/};p' file
    line 1
    line 2
    foo
    REPLACEMENT
    bar
    line 6
    line 7
  2. epcim revised this gist Jul 22, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions sed_regex_keywords.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Character Class Description

    ```
    [[:alnum:]] Alphanumeric [a-z A-Z 0-9]
    [[:alpha:]] Alphabetic [a-z A-Z]
    [[:blank:]] Blank characters (spaces or tabs)
    @@ -11,4 +11,5 @@
    [[:punct:]] Punctuation characters
    [[:space:]] Whitespace
    [[:upper:]] Uppercase letters [A-Z]
    [[:xdigit:]] Hex digits [0-9 a-f A-F]
    [[:xdigit:]] Hex digits [0-9 a-f A-F]
    ```
  3. epcim created this gist Jul 22, 2016.
    14 changes: 14 additions & 0 deletions sed_regex_keywords.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    # Character Class Description

    [[:alnum:]] Alphanumeric [a-z A-Z 0-9]
    [[:alpha:]] Alphabetic [a-z A-Z]
    [[:blank:]] Blank characters (spaces or tabs)
    [[:cntrl:]] Control characters
    [[:digit:]] Numbers [0-9]
    [[:graph:]] Any visible characters (excludes whitespace)
    [[:lower:]] Lowercase letters [a-z]
    [[:print:]] Printable characters (noncontrol characters)
    [[:punct:]] Punctuation characters
    [[:space:]] Whitespace
    [[:upper:]] Uppercase letters [A-Z]
    [[:xdigit:]] Hex digits [0-9 a-f A-F]