#!/usr/bin/sed -nf # Skip comment lines /^@.*$/ b # Count empty lines. Excute this for all empty lines /^$/ { # Exchange pattern and hold space x /^$/ s/^.*$/1/ # Append hold space to pattern space G # copy pattern space to hold space h # /p --> print the new pattern space s/^/ / s/^ *\(......\)\n/\1 / # Exchange pattern and hold space x s/\n.*$// /^9*$/ s/^/0/ s/.9*$/x&/ # copy pattern space to hold space h s/^.*x// y/0123456789/1234567890/ # Exchange pattern and hold space x s/x.*$// # Append hold space to pattern space G s/\n// # copy pattern space to hold space h # exit script b } # If the line is non-empty print out line number of last empty line # end the contents of the non-empty line # Append hold space to pattern space G # Exchange the two lines and create valid output s/\(.*\)\n\(.*\)/\2: \1/ # In the case of the first line the number is not yet initialized s/^:/1:/ # Print pattern space p b