Line of code statistics and professional statistics tools

Source: Internet
Author: User
Tags svn

believe that doing any development of the program developers will encounter a problem, is the number of lines of code statistics. Although there is no substantial significance in the statistics of line count, the complexity of module or system development can be understood from the side.


shell script code line number of common commands

In fact, Baidu or Google, we can easily search a lot of scripts to do statistics, but are not very accurate, the search is as follows:

# Find. -name "*. [CH] ' | Xargs Cat | Wc-l
# Find.-name "*.[ CH] "| Xargs Cat | Grep-v ^$|wc-l
# wc-l ' Find.-name ' *.[ CH] "' |tail-n1


The main problem with the line count of shell script code


To better obtain accurate, good compatibility of the command, do some simple analysis, mainly dealing with the following issues:

1 support Windows line break \ r \ n and Linux line feed \ n Processing (compatibility)

2 support. c/.h file type (extensible)

3 support blank line and annotation character identification (not counting lines of code)

4) Support #include character identification (not counting lines of code)


shell script code line count optimization

# Find. -name "*. [CH] ' | Xargs Cat | Tr-d ' \ r ' | Sed ' s/^[\t]*//g ' | Grep-v "^$" | Grep-v "^/[/*]" | Grep-v "^#" |wc-l

= = "Find." -name "*. [CH] "---iteration directory, looking for a code file that ends with. C and. h, you can consider adding. asm,. cpp,. Java languages

= = "Xargs Cat---Pass the previous command output as an input parameter to the next command

= = "Tr-d ' \ r '---delete \ r characters to ensure consistency of Windows and Linux line breaks

= = "sed ' s/^[\t]*//g '---Remove the beginning of the space, unified identification annotation

= = grep-v "^$"---remove empty lines

= = Grep-v "^/[/*]"---remove "//" and "/**/" Comment line

= = grep-v "^#"---remove the #include line of statistics

= = "Wc-l---row count


the storage knowledge of shell scripts

^ Beginning of a line ^d with opening ^. 1 The third character of the 1 ^ character to be placed in front of the matching character
$ at the end of a line ^$ represents a blank line ^.$ the $ character that matches one line of characters to be placed behind a matching character
* matches any character including 0 characters. Represents a single character
\ can mask a special character \*\.pas here's * is a special character, so that means *.pas this file
Commas can be divided into different matching characters Furu [s,s] means s or s can
-Represents a range, [1-9],[a-z],[a-z] [1-9-A-z] any character or number
A\{2\}b says A has appeared 2 times AAB
A\{4,\}b says a at least 4 times Aaaab,aaaaab
A\{2,4\}b says a appears 2-4 times AAB Aaab Aaaab
[0-9]\{3\}\. [0-9]\{3\}\. [0-9]\{3\}\. [0-9]\{3\} 0-999.0-999.0-999.0-999


Common shell commands for shell scripts

1 TR command to remove blank lines

# cat filename |tr-s ' \ n '

2 sed command to remove blank lines
# cat filename |sed '/^$/d '

3 awk command to remove empty lines
# cat filename |awk ' {if ($0!= "") print} '
# cat filename |awk ' {if (length!=0) print $} '

4) grep command to remove blank lines
# grep-v "^$" filename

5 sed Delete the beginning of a space
# sed ' s/^[\t]*//g '

Description

The first/left is the substitution of the s, and the space is replaced with null.
The first/right is the beginning of the following XX.
The brackets denote "or", either in a space or in the tab. This is the specification of the regular expression.
The right side of the bracket is *, representing one or more.

There is nothing in the second and third \ Middle, which means null.

G means to replace the original buffer (buffer), sed in the processing of strings does not directly deal with the source file, first create a buffer, but add g to the original buffer to replace. The whole idea is to replace one or more body strings with spaces or tabs with null characters.

6 sed Delete end of line space
# sed ' s/[\t]*$//g '

Slightly different from the above is the previous deletion of the ^ character, followed by a dollar sign, which means the end of XX string as an object. However, note that in Ksh, the tab is not \ t but directly into a tab.

7 sed Delete all the blanks
# sed s/[[:space:]]//g


Professional Code Statistics Tools

Professional Code statistics tools, recommended the use of STATSVN, installed the tool, combined with SVN can be a good generation of HTML reports.


The report command is generated as follows:

</pre><pre name= "code" class= "plain" ># mkdir
# SVN log stat >--xml-v
# stat/svn.log. /statsvn.jar Stat/svn.log. -output-dir Stat
# STATSVN stat/svn.log.-output-dir Stat




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.