How to count lines - ignoring blank lines and commented lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to count lines - ignoring blank lines and commented lines
# 1  
Old 05-23-2007
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  
Old 05-23-2007
Code:
awk '!/^#/ && !/^$/{c++}END{print c}' "file"

# 3  
Old 05-23-2007
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 10:17 PM..
# 4  
Old 05-24-2007
much simpler ! : )
Code:
egrep -cv '#|^$' fielname

# 5  
Old 05-24-2007
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  
Old 05-24-2007
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  
Old 05-25-2007
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

 
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

Count duplicate lines ignoring certain columns

I have this structure: col1 col2 col3 col4 col5 27 xxx 38 aaa ttt 2 xxx 38 aaa yyy 1 xxx 38 aaa yyy I need to collapse duplicate lines ignoring column 1 and add values of duplicate lines (col1) so it will look like this: col1 col2 col3 col4 col5 27 xxx 38 aaa ttt ... (3 Replies)
Discussion started by: coppuca
3 Replies

3. Shell Programming and Scripting

Reform Lines in File without blank lines and spaces

Hello All, I have a file with data as below. Each line consists of 21 fields. I am not able to load them back to the database. 50733339,"834","834 ","005010X279A1","N","Y","007977163","0001 ",30,"2110D ","EB ","EB007 ","2 ","Conditional Required Data Element Miss ing... (3 Replies)
Discussion started by: Praveenkulkarni
3 Replies

4. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

5. Shell Programming and Scripting

sed show lines text between 2 blank lines

I have a file like blah blah blah blah this is the text I need, which might be between 1-4 lines, but always has a blank line above and below it, and is at the end of the text file the code tags don't show the trailing blank line. I started by deleting the last blank line with: ... (2 Replies)
Discussion started by: unclecameron
2 Replies

6. Shell Programming and Scripting

Help in replacing two blank lines with two lines of diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. sed "/data/{G;G;}/" filename . In the file, after data tag, two lines got inserted blank lines..... (4 Replies)
Discussion started by: arjun_arippa
4 Replies

7. 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

8. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

9. Shell Programming and Scripting

Count uncommented, blank and source lines in perl

Hi friends, I am working on a perl script to count the commented lines, blank lines and source lines separately. Let me know if you have one. For example i have a file containing the lines: /** * SOURCE CODE */ public class SessionConstants { /** * Code for Session created */... (4 Replies)
Discussion started by: nmattam
4 Replies

10. Programming

to count the number of commented lines in a file

can someone guide me how to have a C pgm to count the number of commented lines? (3 Replies)
Discussion started by: naan
3 Replies
Login or Register to Ask a Question

Featured Tech Videos