![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Removing Blank Lines | dhanamurthy | Shell Programming and Scripting | 3 | 05-07-2008 10:52 PM |
| Eliminate blank lines... | shashi_kiran_v | UNIX for Dummies Questions & Answers | 4 | 09-05-2005 09:39 PM |
| Remove blank lines | osymad | UNIX for Dummies Questions & Answers | 4 | 08-27-2005 03:41 AM |
| delete blank lines or lines with spaces only | vascobrito | UNIX for Dummies Questions & Answers | 3 | 01-13-2004 03:36 AM |
| Blank Lines - End of file | saabir | Shell Programming and Scripting | 4 | 07-15-2003 08:55 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
||||
|
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:
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 |
|
|||
|
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! |
|
|||
|
Quote:
Code:
egrep -cv '#|^$' active_servers > somefilename |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|