If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > remove blank lines from a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Feb 2004
Posts: 143
remove blank lines from a file

i have a data file (.dat) that has 10000 lines out of which 8 lines are blank. what is the command to remove these blank lines from the file?
Thanks
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 320
Code:
# Remove empty lines 
sed '/^$/d' input_file

# Remove empty or blank lines (two methods)
sed '/^[[:spaces:]]$/' input_line
awk 'NF>0' input_line
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Feb 2004
Posts: 143
Thank you very much. It worked.

Can you also guide me how to find out the total number of
blank or empty lines in an input file ? Thanks
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 320
Code:
awk 'NF>0 {count++} END { print count}' input_line
__________________
Jean-Pierre.
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Feb 2004
Posts: 143
you are awesome, thanks
Reply With Quote
  #6 (permalink)  
Old
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally posted by aigles
Code:
awk 'NF>0 {count++} END { print count}' input_line
The above would count the number of non-blank lines. To count the number of blank lines, subtract from the record count.
Code:
awk 'NF>0 {count++} END { print NR-count}' yourFile
Another way would be to use grep -c.
Code:
grep -c "^[ \t]*$" yourFile
Damian
Reply With Quote
  #7 (permalink)  
Old
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 320
Oops .... Good shooting Damian !
__________________
Jean-Pierre.
Reply With Quote
  #8 (permalink)  
Old
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 320
I forgot to give the correction :
Code:
awk 'NF==0 {count++} END { print count}' input_line
__________________
Jean-Pierre.
Reply With Quote
  #9 (permalink)  
Old
Registered User
 
Join Date: Jan 2013
Posts: 4
Hi ,

I used grep -c "^[ \t]*$" File_Name and getting answer as 0 ...

does that mean there are no lines between records..

Please tell me how to delete spaces between records from dat file .
Neha
Reply With Quote
  #10 (permalink)  
Old
Registered User
 
Join Date: Jan 2013
Posts: 4
Hi ,

I used grep -c "^[ \t]*$" File_Name and getting answer as 0 ...

does that mean there are no lines between records..

Please tell me how to delete spaces between records from dat file .
Neha
Reply With Quote
  #11 (permalink)  
Old
Programming since 1BC
 
Join Date: Sep 2009
Location: Ontario
Posts: 934
Does the file have newlines in it?
.dat files are generally created by an application program, and may appear to operating system utilities as a single line, that is, the records are fixed length, and do not have record separators.
Use 'hexdump' to determine if there are record separators (a line feed is 0a in hexadecimal.)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On