sed one liner to Delete blank lines - Help required


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed one liner to Delete blank lines - Help required
# 1  
Old 04-27-2015
sed one liner to Delete blank lines - Help required

Hi,

Code:
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

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

the output is

Code:
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
# 2  
Old 04-28-2015
Code:
Read Pattern      Commands       Operations/Comments  -                                            (New)  Pattern Space that after command

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

sed one Liner inverse range match-help required

cat test.txt a c d e g (2 Replies)
Discussion started by: TomG
2 Replies

3. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

4. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

5. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

6. UNIX for Dummies Questions & Answers

How get only required lines & delete the rest of the lines in file

Hiiii I have a file which contains huge data as a.dat: PDE 1990 1 9 18 51 28.90 24.7500 95.2800 118.0 6.1 0.0 BURMA event name: 010990D time shift: 7.3000 half duration: 5.0000 latitude: 24.4200 longitude: 94.9500 depth: 129.6000 Mrr: ... (7 Replies)
Discussion started by: reva
7 Replies

7. Shell Programming and Scripting

Sed one-liner to print specific lines?

I need to print specific lines from a file, say 2-5, 8, 12-15, 17, 19, 21-27. How do I achieve this? (2 Replies)
Discussion started by: Ilja
2 Replies

8. Shell Programming and Scripting

Why cant i delete blank lines?

I have a sed pipeline: myVar=$(cat $FILE | sed -n '/regex/,/regex/{/regex/d;p}' | sed -n '/regex/!p' | sed -e s/*:// | sed /regex/,+8d \ ) sed '/^$/d' sed '/./!d' And i've tried to add that in a different order rather then just on the end..Why isnt it deleting all the blank... (2 Replies)
Discussion started by: omgsomuchppl
2 Replies

9. Shell Programming and Scripting

delete blank lines with sed

I have a text file with blank lines fullfilled with spaces and others only containing the "Enter" caracter, the \012. I would like to eliminate all them with the sed command. Is it possible? making: sed '/^$/d' <file should delete the blank lines but doesn't work for the lines that only... (2 Replies)
Discussion started by: tmxps
2 Replies

10. UNIX for Dummies Questions & Answers

delete blank lines or lines with spaces only

hi there i'm trying to delete blank lines and or lines with spaces only from a series of files in an directory. to do so, i'm using this: for files in `ls /users/myname/pesop* 2>/dev/null` do grep -v ^$ $files > newfile mv newfile $files done now, this works great for blank lines but... (3 Replies)
Discussion started by: vascobrito
3 Replies
Login or Register to Ask a Question