A shell command to get the count of code lines

Source: Internet
Author: User

The most crude version

Shell code find. -name "*.java" |xargs wc-l|grep "Total" |awk ' {print $} ' find. -name "*.java" |xargs cat|wc-l #Output: 37634

The above two sentences are the same, but the second one is to use cat to merge the contents of multiple Java files into the output. and then statistics.

But two have a common problem--even the blank line in the file to count into a row.

Remove empty row version

Shell code find. -name "*.java" |xargs cat|grep-v ^$|wc-l #Output: 36335 This version is a bit more reliable, you can see the results of the empty row after the result has changed: 36335, compared to the previous version of the 1299 rows of empty line ...

However, Java code also has comments ah ... Fuck, you have to kill the note.

Remove annotation Version

Shell code # Exclude the lines begin with//find. -name "*.java" |xargs cat|grep-v-E ^$-e ^\s*\/\/.*$|wc-l #Output: 36064

Well, this method finds that the comment beginning with//has a 36335-36064=271 line. It should be noted here that the comment line does not necessarily take//as the beginning, perhaps n a space after the start of the//, so the match needs to add the above ^\s* at the beginning.

It's done. Nonsense ...

Java comments Several styles, and then show you this very common annotation, or will be in the statistical results of the Java code/** * @author XXX 2012-6-15 3:19:47 * *

What the hell. This annotation I really did not think how to easily remove, simple and rough method but can be in the above grep parameters add 3 more regular, respectively, divided by/**, *, * *, the beginning of the line.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.