Removing blank lines from a file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Removing blank lines from a file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-18-2012
raosr020 raosr020 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 1 September 2014, 10:42 PM EDT
Posts: 21
Thanks: 11
Thanked 0 Times in 0 Posts
Tools Removing blank lines from a file

Hi All,

How do i remove continuos blank lines from a file.

I have a file with data:


Code:
 
abc;
def;
ghi;
 
 
jkl;
mno;
pqr;

In the above file, there are two blank lines. I want to remove, one out of them.

My output should look like:

Code:
abc;
def;
ghi;
 
jkl;
mno;
pqr;

With the below command, all the blank lines are getting deleted. I don't want that. I have to remove one out of two blanks.

Code:
 
 
sed -i '/^$/d' my_file

Sponsored Links
    #2  
Old 12-18-2012
bmk bmk is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 31 October 2013, 9:16 AM EDT
Posts: 262
Thanks: 1
Thanked 22 Times in 22 Posts
Try like...

Code:
  awk 'NF>0' test.txt

Sponsored Links
    #3  
Old 12-18-2012
raosr020 raosr020 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 1 September 2014, 10:42 PM EDT
Posts: 21
Thanks: 11
Thanked 0 Times in 0 Posts
The above command is removing all the blank lines. But i want to keep only one blank line if more than one blank lines avail. I want to remove all other blank lines.

---------- Post updated at 06:22 PM ---------- Previous update was at 06:17 PM ----------


Code:
awk '/^$/{ if (! blank++) print; next } { blank=0; print }' my_file

or

Code:
 
awk 'NR==1 || NF || prvNF {print} {prvNF=NF}' my_file

Any of the above codes are working fine for me..
Thank you all!!!!!!!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing blank lines not working MaindotC UNIX for Dummies Questions & Answers 12 08-20-2012 09:00 AM
Removing blank lines in a file ashok.k UNIX for Dummies Questions & Answers 3 04-27-2010 02:56 AM
Removing blank lines from comma seperated and space seperated file. pinnacle Shell Programming and Scripting 11 05-08-2009 11:58 AM
removing duplicate blank lines rameezrajas Shell Programming and Scripting 8 07-31-2008 08:39 AM
Removing Blank Lines dhanamurthy Shell Programming and Scripting 3 05-08-2008 01:52 AM



All times are GMT -4. The time now is 09:36 PM.