Flow of control in sed commands


Default
Commands to introduce additional loops or branching.
  • b - b label Unconditional branch to sed command line Id's by label. Combined with address/regular expressions for conditional action.
  • t - t label Branch to sed command line ID's by label if previous edit command successful.
  • :label - label line Labels a point in the sed command sequence that can be jumped to with the b or t commands. A branch or test command with no label will force execution to end of command sequence, similar to continue in shell loops. The following will change all but the 1st occurence of President to Mr. Once the line has only one occurence of President, the substitute fails and the t is not taken. If you were to use a b (branch), this would be an endless loop.
    :again
    s/President/Mr./2
    t again
    
    This will do the same using branch but requires a pre-test to enter the edit block.
    :again
    /President.*President/ {
      s/President/Mr./2
      b again
    }
    
    #!/bin/sh
    # This sed script will delete multiple blank lines between non-blank lines
    #   while leaving a single blank line if one or more existed.
    #
    #1st run a sed that removes any white space from a blank line and start
    # feeding it into a second sed.
    #  
    #2nd sed
    #
    #  :again  # reentry point for internal branch
    #  If line is empty
    #  then
    #    read then next line in merging both lines in the edit buffer.
    #    (New line should still be in buffer)
    #
    #    If line on both sides of new-line is empty,
    #      remove the new-line character by substituting null.  
    #         (Basically merge lines.)
    #      branch back to again label
    #
    #    endif
    #  endif 
    #  output the line.
    #
    # Call this program with the file to edit as 1st argument,
    #    use redirection to save results.
    #
    sed -e 's/^[ 	]*$//' $1 | sed '
    :again
    # if line is blank
    /^$/{
      N              # fetch then next line
      /^\n$/{        # if both lines are blank
         s/^\n//     #   remove the new line (merge) from the buffer.
         b again     #   branch back to begin.
      }
    #else added line not blank, complete edit pass and print out buffer
    }' 
    

    #!/bin/sh
    # Deletes the blank line from double spaced lines
    #   But will leave a single blank line if multiple consecutive 
    #   blank lines encountered.
    #
    # e.g  file is double spaced with additional blank lines between paragraphs
    #
    # The 1st sed clears any 'blank' line with 'invisible' tabs or spaces. This
    #   could be done in the main edit but would  have to be run at the start and
    #   after each N
    #
    # If there are multiple blank lines at the end of the file, this will not
    #   delete the last 2
    #
    sed -e 's/^[ 	]*$//' $1 | sed '
    :again
    # Check for blank lines
    /^$/{              # if line is blank
      N                #   join next line
      /^\n$/ !{        #   if the next line joined is NOT blank 
         s/^\n//       #     delete the new line, making it a single line.
         b again       #     Return to begin
      } 
    
      :dblank
      #  Check and remove repeated blank lines
      /^\n$/{          # else if both lines blank. 
        N              #   join next line
        /^\n\n$/{      #   if both lines blank 
          s/^\n//      #     remove the 1st newline 
          b dblank     #     branch back to dblank 
        }
        s/^\n// i      #   deletes remaining new line, making it a single line.
                       #   although the new line is gone, if the line is blank 
                       #     it will still be 1 blank line.
      }
    }'