Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-04-04, 13:28
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 136
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  
Old 03-04-04, 14:15
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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  
Old 03-04-04, 14:22
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 136
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  
Old 03-04-04, 14:28
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Code:
awk 'NF>0 {count++} END { print count}' input_line
__________________
Jean-Pierre.
Reply With Quote
  #5  
Old 03-04-04, 15:05
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 136
you are awesome, thanks
Reply With Quote
  #6  
Old 03-05-04, 05:57
Damian Ibbotson Damian Ibbotson is offline
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  
Old 03-05-04, 06:04
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Oops .... Good shooting Damian !
__________________
Jean-Pierre.
Reply With Quote
  #8  
Old 03-05-04, 08:13
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
I forgot to give the correction :
Code:
awk 'NF==0 {count++} END { print count}' input_line
__________________
Jean-Pierre.
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump