Flow of control in edit commands
b - branch. sed supports an internal loop structure. With it you can bypass or repeat certain edits. A branch has two part, a label or target and the branch command with the designated label target. Branches can be designed to be conditional or unconditional, although it is almost always in some form conditional.

t - tests to see if the previous edit actually occurred. This is basically a version of branch that test status of last edit executed rather than an address. The test and branch may target the same label.

:label

  • The label can be up to 7 characters preceded by a colon. Although the Posix standard allows for an unlimited label length.

  • The label must be on a line by itself with no spaces after.

  • Each label must be unique.

  • A label may be placed anywhere in the sed command list.

  • A label may be targeted by any branch or test command.

    [address]b[label]

  • A branch with no label target will branch to the end of the command list.

  • The branch command may be preceded by an address or regular expression. This acts as a test condition for the branch.

    sed -e '
    # print the line pre-edit
    p
    
    # if the line contains 2 or more instances of the word house
    #  change all but 1st to home
    :again
    s/\<house\>/home/2
    t again
    
    # if the contains a series of slashes, convert all but the
    # 1st and last to star
    :sagain
    s#/#*#2
    /\/\// b again
    p
    
    :pagain
    s/\*\*/*|/
    t pagain' /home/hopper/berezin/Data/myhouse
    

    The following removes single blank lines between lines of text. If there are more than 1 consecutive blank lines, they are ignored.

    #1st make sure blank lines are empty
    sed -e 's/^[    ]*$//' /home/hopper/berezin/Data/linedata | sed  -e '
    #If line as text, skip edits.
    /^./ b
    
    # fetch next line
    N
    
    # If both lines blank, skip edits.
    /^\n$/b
    
    # if 2nd line not empty
    /^\n..*$/ {
    
      # Delete new line
      s/^\n//
    }
    ' | less
    
    
    A word on multi-edit sed with embedded comments on the command line, bash is very forgiving of this style of command entry. If you find you need to work in a different shell, you may want to keep all your edits in a file and use sed -f to fetch them.