Flow of control in sed commands
Default
:again s/President/Mr./2 t again |
: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.
}
}'
|