sed one liner to Delete blank lines - Help required

Hi,

cat test.txt
BlankLine
BlankLine
BlankLine
BlankLine
ello
hi
helo
BlankLine
BlankLine
heylo
BlankLine
BlankLine
BlankLine
done
BlankLine
BlankLine
BlankLine

The above is the content of the file.(blank lines in between lines,at the begining of file and at the end of the file)

I am applying the following sed command

 sed '/^$/N;/\n$/D' test.txt

the output is

BlankLine
ello
hi
helo
BlankLine
heylo
BlankLine
done

Note : inorder to represent a blank line i have used 'BlankLine'.
My doubt is how 1 blank line in begining and 1 blank line in between the lines are preserved. Also all blank lines at the end are removed.

Can anyone please explain this.?

Thank You

Read Pattern      Commands       Operations/Comments  -                                            (New)  Pattern Space that after command
1.line -> \n       /^$/   (if the my Pattern is a blank line) then                                 \n$     (1.line [\n])
                          YES my Pattern space is a blank line -> "\n")

2.line -> \n        N;     (Appends next line to our pattern space)                               \n\n$  *(1.line [\n] and 2.line [\n])           

     
                   \n$/   (if the end of *(its means -> $) the my Pattern's contains "\n")              
                   D;     (Delete first portion of the pattern space until the "\n")                \n$ (current Pattern)
                                       so deletes at the end "\n" 
					    *(deleted the second "\n" in the pattern space)   
                                   and go on next cycle *(begin the execute command again ) 
				over new Pattern so both new Pattern and readed the file 
						would be our NEW PATTERN )

----------------------------------------------------------------------------------------------------------------------------------------------        


*(NEW PATTERN 
from Pattern Space)                   

[\n] *                  *-1 (last state the Pattern space was -> "\n$" )
                        *-2 (look at end of the New Pattern space table)

                 /^$/   *-3 (YES my Pattern space is a blank line -> "\n")
                                                        
3.line -> \n      N;     *(Appends next line *(3.line) to our Pattern space)                        \n\n$ (current Pattern and 3.line [\n])
            
                 /\n$/  *-4 (again if the end of the current Pattern space contains "\n" 
                                YES it was(\n\n$))

                 D;     *-5 (Delete first portion of the pattern space until the "\n")                \n$ (current Pattern)                       
                             so it is "\n\n$" --> it was "\n$" after the "D" command

-----------------------------------------------------------------------------------------------------------------------------------------------

[\n] *                  *-1 *-2 *(same comments )


                 /^$/   *-3


4.line -> \n     N;     *(Appends next line *(4.line) to our Pattern space)                    \n\n$ (current Pattern and 4.line [\n])

                 /\n$/  *-4
                          
                 D;     *-5                                                                        \n$ (current Pattern)

-----------------------------------------------------------------------------------------------------------------------------------------------

[\n] *                  *-1 *-2 *(same comments )
                       
                 /^$/   *-3 


5.line -> ello   N;     *(Appends next line *(5.line) to our Pattern space)                     \nello$ (current Pattern and 5.line [ello])

                 /\n$/  *-6 (the condition is not true then skip "D" command ) 
				             
			*-7 default action -> (PRINT Pattern and read the NEXT line)          
                            				 

                    
					!!! THERE IS A PRINT !!!
					  ====================  
						  \n
						  ello   
						  
	**  (these are in the sed buffer and will be PRINTed at the end of all commands, 
	*** (i.e. -> when Pattern space till emptied -> look at the end of the cycle )
	
-----------------------------------------------------------------------------------------------------------------------------------------------

6.line -> hi	 /^$/	(the condition is not true then next to other commands)                        hi$  (current Pattern)
                 
		/\n$/  *-6 *-7 (same comments )
				 
				       
					!!! THERE IS A PRINT !!!
					 ====================  
						  hi

-----------------------------------------------------------------------------------------------------------------------------------------------

7.line -> helo   /^$/	(the condition is not true then next to other commands)                        helo$  (current Pattern)
                 
		 /\n$/  *-6 *-7 (same comments )
					   
					 
					!!! THERE IS A PRINT !!!
					 ====================  
						  helo

-----------------------------------------------------------------------------------------------------------------------------------------------

8.line -> \n     /^$/	 YES		                                                        \n$  (current Pattern)
                 
9.line -> \n	 N;      (Appends next line *(9.line) to our Pattern space)                      \n\n$ (current Pattern and 9.line [\n])
				 
		 /\n$/  *-4
						
		 D;     *-5                                                                      \n$ (current Pattern)                       

-----------------------------------------------------------------------------------------------------------------------------------------------                         
[\n] *
                        *-1 *-2 *(same comments )
                       
                 /^$/   *-3 			                                                 \n$  (current Pattern)
                 
10.line -> heylo N;     *(Appends next line *(10.line) to our Pattern space)                   \nheylo$ (current Pattern and 10.line [heylo])
				 
		 /\n$/  *-6 *-7 (same actions )
						
				 
				        !!! THERE IS A PRINT !!!
					 ====================  
						  \n
						  heylo
				 
-----------------------------------------------------------------------------------------------------------------------------------------------                         
11.line -> \n            *-1 *-2 *(same comments )
                       
                 /^$/    *-3 				                                        \n$  (current Pattern)
                 
		  N;     *(Appends next line *(12.line) to our Pattern space)              \n\n$ (current Pattern and 12.line [\n])
				 
		  /\n$/  *-4 
				 
		  D;     *-5                                                                  \n$ (current Pattern)
						
					
-----------------------------------------------------------------------------------------------------------------------------------------------

[\n] *
                	 *-1 *-2 *(same comments )
                       
               /^$/      *-3 				                                               \n$  (current Pattern) 
                 
13.line -> \n    N;     (Appends next line *(13.line) to our Pattern space)                \n\n$ (current Pattern and 13.line [\n])

               /\n$/     *-4 
						
		D;       *-5                                                            \n$ (current Pattern)
				 
-----------------------------------------------------------------------------------------------------------------------------------------------

[\n] *
                	 *-1 *-2 *(same comments )
                       
                 /^$/    *-3 				                                               \n$  (current Pattern)
                 
14.line -> done  N;     (Appends next line *(14.line) to our Pattern space)               \ndone$ (current Pattern and 14.line [done])

                 /\n$/   *-6 *-7 (same actions ) 
						
                     
					!!! THERE IS A PRINT !!!
					 ====================  
						  \n
						  done

-----------------------------------------------------------------------------------------------------------------------------------------------	

15.line -> \n           *-1 *-2 *(same comments )

	        /^$/    *-3 	                                                             \n$  (current Pattern)

16.line -> \n    N;     (Appends next line *(16.line) to our Pattern space)                   \n\n$ (current Pattern and 16.line [\n])

                /\n$/   *-4 
						
		D;      *-5                                                                    \n$ (current Pattern)
				 
-----------------------------------------------------------------------------------------------------------------------------------------------

[\n] *
                	 *-1 *-2 *(same comments )
                       
                 /^$/    *-3 			                                          \n$  (current Pattern)
				 
17.line -> \n    N;     (Appends next line *(17.line) to our Pattern space)                   \n\n$ (current Pattern and 17.line [\n])

		 /\n$/   *-4 
						
	         D;      *-5                                                                 \n$ (current Pattern)
				 
				  
				       !!! THERE IS A PRINT !!!
					 ====================  
						  \n
	
	
	*** ( Pattern space till emptied because of we arrived the EOF then close the file and ended the SED )
	
	and default action works (PRINT THE SED'S PATTERN BUFFER )
	
	                  
					 
						  
-----------------------------------------------------------------------------------------------------------------------------------------------

				 so all prints echoes to screen
				           RESULT
				 =================================
				 
				 \n
                                 ello
                                 hi
                                 helo
                                 \n
                                 heylo
                                 \n
                                 done
                                 \n

regards
ygemici