Using cloc (count Lines of Codes) result

1.8k Views Asked by At

I am writing a script for my research, and I want to get the total number of lines in a source file. I came around cloc and I think I am going to use it in my script.

However, cloc gives result with too many information (unfortunately since I am a new member I cannot upload a photo). It gives number of files, number of lines, number of blank lines, number of comment lines, and other graphical representation stuff.

I am only interested in the number of lines to use it on my calculations. Is there a way to get that number easily (maybe by performing some options in command line (although I went through the available options and didn't find something useful for my case))?

I thought to do regular expression on the result to get the number; however, this is my first time using cloc and there might be a better/professional way of doing it.

Any thought?

Regards, Arwa

3

There are 3 best solutions below

0
On

@BinaryMee and @engineersmnky thanks for your response.

I tried two different solutions, one using "readlines" got the answer from @gicappa

Count the length (number of lines) of a CSV file?

the other solution using cloc. I ran the command

%x{perl #{ClocPath} #{path-to-file} > result.txt} 

and saved the result in result.txt

cloc returns result in a graphical form (I cannot upload image), it also reports number of blank lines, comment lines, and code lines. As I said, I am interested in code lines. So, I opened the file and used regular expression to get the number I needed.

content = File.read("#{path}/result.txt")
line = content.scan(/(\s+\d+\s+\d+\s+\d+\s+\d+)/)
total = line[0][0].split(' ').last

content here will have the content of a file, then line will get this line from the file:

C# 1 3 3 17

C# is the language of a file, 1 is number of files, 3 is number of blank lines, 3 is number of comment lines, and 17 is number of code lines. I got the help of the format from the script of cloc. total then will have number 17.

This solution will help if you are reading a specific file only, you need to add more solutions if you are reading the lines of more than one file.

Hopefully this will help who needs it.

Regards, Arwa

0
On

I am not sure about CLOC. But it is worth using default shell command. Please have a look at this question.
To get number of lines of code individually

find . -name '*.*' | xargs wc -l

To get total number of lines of code in a directory.

(find ./ -name '*.*' -print0 | xargs -0 cat) | wc -l 

Please note that if you need number of lines from files with specific extension you could use *.ext.
*.rb, if it is ruby.

0
On

For something very quick and simple you could just use:

Dir.glob('your_directory/**/*.rb').map do |file| 
  File.foreach(file).count
end.reduce(:+)

This will count all the lines of .rb files in your_directory and it's sub directories. Although I would recommend adding some handling for blank lines as well as comment lines. For more on Dir::glob