Count uncommented, blank and source lines in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count uncommented, blank and source lines in perl
# 1  
Old 03-19-2010
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:
Code:
 /**
 * SOURCE CODE
 */
 
public class SessionConstants {
  /**
   * Code for Session created
   */
public final static int CREATED_CODE = 0;
 
  /**
   * Constants defining the Session type
   */
  public enum SessionType {
    DC_SES, LC_SES, SM_SES
  }
}

The script should read the file and count the Commented lines(/* */), blank lines and actual source lines. The expected output should be, Commented lines=9, Blank lines=2, Source Lines=6. Thanks in advance.

Last edited by pludi; 03-19-2010 at 04:34 AM.. Reason: code tags, please...
# 2  
Old 03-19-2010
MySQL

Use this ,

Code:
#!/usr/bin/perl

use strict;
use warnings;

open FH,"<file";
my $space=0;
my $cmd=0;
my $ind=0;
my $code=0;
while(<FH>)
{

    if(/^$/)
    {
        $space++;
    }
    elsif(/\/\*.*\*\//)
    {
        $cmd++;
    }
    elsif(/\/\*/)
    {
        $cmd++;
        $ind=1;
    }
    elsif(/\*\//)
    {
        $cmd++;
        $ind=0;
    }
    elsif($ind==1)
    {
        $cmd++;
    }
    else
    {
        $code++;
    }
}

print " Commented Lines : $cmd\n";
print " Code Lines : $code \n";
print " Empty Lines : $space \n";

# 3  
Old 03-23-2010
Count uncommented, blank and source lines in perl

Thanks karthigayan for the effort, but the result is partially correct. Commented Lines : 9 is exactly correct. Code Lines : 8 is wrong, it must pick only 6 lines from the example i had given. Empty Lines : 0 but it must be 2. Please explain the flow if i am missing anything. Thanks again.
# 4  
Old 03-23-2010
cloc counts blank lines, comment lines, and physical lines of source code: CLOC -- Count Lines of Code
# 5  
Old 03-29-2010
Count uncommented, blank and source lines in perl

My actual requirement was to execute a cleartool diff command on two files and the output of the diff command is shown below.

Code:
********************************
<<< file 1: M:\nm_int\SesCon.java@@\main\ga_jc\0
>>> file 2: M:\nm_int\SesCon.java@@\main\ga_jc\LATEST
********************************
--[after 0]--|------[inserted 1-97]-------
                 -| /**
                  |  *  SOURCE CODE
                  |  *  
                  |  */
                  | public class SessionConstants {
                  | 
                  |   /**
                  |    * Code for Session created
                  |    */
                  |   public final static int CREATED_CODE+
                  | 
                  |   /**
                  |    * Code for Session modified
                  |    */
                  |   public final static int MODIFIED_COD+
                  | 
                  |   /**
                  |    * Code for client log out
                  |    */
                  |   public final static int LOGOUT_CODE +
                  | 
                  | 
                  |   /**
                  |    * Constants defining the Session ty+
                  |    */
                  |   public enum SessionType {
                  |     DCFM_SESSION, LIC_SESSION, SMIA_SE+
                  |   }
                  | }
                  |-

This output must be used to count all the number of commented, blank and source lines. I tried the script given karthigayan but it doesnt work for blank aswell as source lines.

Thanks in advance.

Last edited by pludi; 03-29-2010 at 04:18 AM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

2. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

3. Shell Programming and Scripting

UNIX command to count blank lines in a file in DOS format

Hi Team, The content of the file is as follows. asdf 234 asdf asdf dsfg gh 67 78 The file is in DOS format (not in Unix Format). The file is transferred to Unix. I need a unix command to check the number of blank lines in a input (comming from Windows). If it is greater than... (4 Replies)
Discussion started by: kmanivan82
4 Replies

4. Shell Programming and Scripting

Perl : blank lines are displayed in the output after deleting few rows from excel

I am working on an assignment to pull all the records from excel sheet programatically and use the data for further calculations. In this process, I first defined 10 records in excel sheet and executed the below code. In the first run it is OK. But after deleting last few rows in excel sheet and... (0 Replies)
Discussion started by: giridhar276
0 Replies

5. Shell Programming and Scripting

perl count lines with certain word int

Hi Guys I have a text file that contains the message like this /var/log/messages.all-20120401: Mar 26 12:12:23 brent kernel: NVRM: Xid (0003:00): 43, 0005 00009097 00000000 00000000 00001b0c 1000f010 /var/log/messages.all-20120401: Mar 27 20:42:40 brent kernel: NVRM: Xid (0003:00): 43,... (4 Replies)
Discussion started by: ab52
4 Replies

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

7. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

8. Shell Programming and Scripting

PERL: removing blank lines from multiple files

Hi Guru's , I have a whole bunch of files in /var/tmp that i need to strip any blank lines from, so ive written the following script to identify the lines (which works perfectly).. but i wanted to know, how can I actually strip the identified lines from the actual source files ?? my... (11 Replies)
Discussion started by: hcclnoodles
11 Replies

9. UNIX for Dummies Questions & Answers

Perl/shell script count the lines

Hi Guys, I want to write a perl/shell script do parse the following file input file content NPA-NXX SC 2084549 45 2084552 45 2084563 2007 2084572 45 2084580 45 3278411 45 3278430 45 3278493 530 3278507 530... (3 Replies)
Discussion started by: pistachio
3 Replies

10. UNIX for Dummies Questions & Answers

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... (6 Replies)
Discussion started by: kthatch
6 Replies
Login or Register to Ask a Question