The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing Blank Lines dhanamurthy Shell Programming and Scripting 3 05-08-2008 01:52 AM
Eliminate blank lines... shashi_kiran_v UNIX for Dummies Questions & Answers 4 09-06-2005 12:39 AM
Remove blank lines osymad UNIX for Dummies Questions & Answers 4 08-27-2005 06:41 AM
delete blank lines or lines with spaces only vascobrito UNIX for Dummies Questions & Answers 3 01-13-2004 07:36 AM
Blank Lines - End of file saabir Shell Programming and Scripting 4 07-15-2003 11:55 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2007
kthatch kthatch is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 28
How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines?

I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end.

Here is an example of what I would like my output to look like:

##comment##
line1
line2
line3
line4
line5
line6

6 lines

##comment##
line7
line8
line9
line10
line11
line12
line13
line14
line15
line16
line17

11 lines

##comment##
line18
line19
line20
line21
line22

5 lines

##comment##
line23
line24
line25
line26
line27
line28
line29
line30
line31
line32
line33
line34
line35

13 lines

35 total lines

Thank you in advance, for your help!
  #2 (permalink)  
Old 05-23-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
Code:
awk '!/^#/ && !/^$/{c++}END{print c}' "file"
  #3 (permalink)  
Old 05-23-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,190
Quote:
Originally Posted by ghostdog74
Code:
awk '!/^#/ && !/^$/{c++}END{print c}' "file"
to extend that somewhat(untested):

Code:
awk '!/^[ \t]*(#|$)/ {c++; ct++} /#/ && c { printf "%s\n\n\n%s", c, $0; c=0} END { print ct}' file

Last edited by reborg; 05-23-2007 at 08:17 PM..
  #4 (permalink)  
Old 05-24-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,930
much simpler ! : )
Code:
egrep -cv '#|^$' fielname
  #5 (permalink)  
Old 05-24-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 699
Hi, kthatch.

You did an excellent job of telling us how the output should look.

However, you did not specify how the sections are separated from each other, nor what you consider a comment - a string at the beginning of a line, a string anyplace in a line, etc.

There is a standard utility nl which knows about sections:
Quote:
-d, --section-delimiter=CC
use CC for separating logical pages
excerpt from man nl
There is also csplit, which can split a file into pieces, based on the occurrence of a regular expression.

Many of the solutions offered are probably adaptable to whatever your file format is, but you may get more on-point suggestions if you are more precise ... cheers, drl
  #6 (permalink)  
Old 05-24-2007
kthatch kthatch is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 28
I tried each of these suggestions and neither worked.

Results:

#egrep -cv '#|^$' active_servers
this displayed the total but on screen only, not in the file

#awk '!/^#/ && !/^$/{c++}END{print c}' active_servers
same as above

#awk '!/^[ \t]*(#|$)/ {c++; ct++} /#/ && c { printf "%s\n\n\n%s", c, $0; c=0} END { print ct}' active_servers
awk: syntax error near line 1
awk: bailing out near line 1

To provide more detail, I have a script that produces the output above (in my original post) to a file - less the totals that I am seeking help with. When I refer to comments, I mean lines that are preceded with ## and the sections are separated with a blank line as a result of this "sed '/this/{x;p;x;}'" in my script.

Thanks again! More suggestions would be welcome!
  #7 (permalink)  
Old 05-25-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,930
Quote:
#egrep -cv '#|^$' active_servers
this displayed the total but on screen only, not in the file
If you had rather need the output on file, redirect that.

Code:
egrep -cv '#|^$' active_servers > somefilename
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0