How to count the number of lines of code

Source: Internet
Author: User
How to count the number of lines of code
  • Linux provides many practical tools, even on Android, which are included in the transplanted busybox.
  • According to the kiss concept, these tools have a single function. However, by combining these tools, we can calculate the amount of code.
Count the number of lines of code
  • The process is as follows: first use the find command to find all source code files with the correct suffix names, and then use the WC command to count the number of lines. The number of files may be too large. To prevent errors, use the xargs command to split the parameter list. You can use sort to sort the final result.
  • Command for counting the number of lines in all C source code:find -name *.c | xargs wc -l |sort -r. Results are arranged in reverse order.
  • find -name *.c | xargs wc -l |sort -k2Results are arranged by file name
  • To calculate the C ++ code, Set*.cChange*.ccYou can. If C ++ uses the CPP extension name, change it*.cpp
Detailed description of the commands used
  • The find command recursively searches for directories. If any file makes the expression true, it is placed in the result.
  • Usagefind [path...] [expression]
  • Path specifies the directory range to be searched. The default value is the current directory.
  • Expression is usually a test condition. True or false is returned.
  • -name patternThe file name conforms to the shell regular expression.
  • -path patternThe file path name conforms to the shell regular expression.
  • -exec order {}Execute the command. If the return value is 0, it is true.
  • Example:find -name *.cSearch for files suffixed with C
  • Example:find . -exec sleep {1}Display an object every 1 s
WC command
  • WC stands for wordcount and is used for statistics files.
  • Usagewc [-lwc] filename
  • L, W, and C indicate the number of rows, words, and characters respectively. All three are counted by default.
  • Example:wc -l file1 file2Count the number of rows in two files
Xargs command
  • Xargs is used to capture the output of the previous command in the pipeline, create a segmentation parameter table, and execute another command.
  • If the parameter column generated by the find command is too long, you can use the xargs command to pass only some parameters at a time to prevent overflow errors in some systems.
  • Xargs usually uses spaces or line breaks to separate parameters,-0Option to ensure that the parameter column contains spaces, line breaks, and so on.
  • Example:find /tmp -name core | xargs -0 /bin/rm -f. Set/tmpAll files named core in the folder are passed to the RM command and deleted forcibly.-0Option to ensure that the file name contains line breaks or spaces.
Sort command
  • Sort is used to sort different texts in behavior units.
  • The default sorting method is ascending,-rOption can be changed to descending order
  • -nThe options are sorted by the value of the number, as shown in figure1 2 10 20If this option is not enabled, the sorting result is1 10 2 20
  • -fCase Insensitive
  • -tSet the delimiter between columns,-kSort by Column
  • Example:sort -n -f -k 2 test.txt. Sort the rows in test.txt by the second column, case insensitive
Redirection
  • The statistical result of the number of rows can be saved to a text file.> count.txtYou can.
  • Standard input, output, and error output are represented by 0, 1, and 2, respectively.
  • Input redirection use the input redirection Operator<, You can use the content in the file as the input of the program. For example:./a.out < test.inThe test. In file contains the test input of the program.
  • Output redirection can write the output of a program to a file and use the output redirection operator.>.
    • Example:ls 1>/dev/null 2>/dev/nullOutput and error output are not displayed. No space is allowed between 1, 2, and>.
    • Example:ls 2> &1> t.txtThe error output is redirected to the standard output, while the standard output is not redirected. Because the standard output content is written to t.txt, the error output remains on the screen.
    • Example:ls 1> t.txt 2> &1Write both standard output and error output to t.txt.
MPs queue
  • MPs queue usage|Indicates
  • The function is to convert the standard output of the previous command to the standard input of the second command.
  • The second command must be read from the standard input, as shown in figurelsCommand does not meet the requirements

 
 

For more information, see focustc. The blog address is http://blog.csdn.net/caozhk.
 
 

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.