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 -k2
Results are arranged by file name
- To calculate the C ++ code, Set
*.c
Change*.cc
You 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.
- Usage
find [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 pattern
The file name conforms to the shell regular expression.
-path pattern
The 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 *.c
Search 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.
- Usage
wc [-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 file2
Count 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,
-0
Option to ensure that the parameter column contains spaces, line breaks, and so on.
- Example:
find /tmp -name core | xargs -0 /bin/rm -f
. Set/tmp
All files named core in the folder are passed to the RM command and deleted forcibly.-0
Option 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,
-r
Option can be changed to descending order
-n
The options are sorted by the value of the number, as shown in figure1 2 10 20
If this option is not enabled, the sorting result is1 10 2 20
-f
Case Insensitive
-t
Set the delimiter between columns,-k
Sort 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.txt
You 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.in
The 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/null
Output and error output are not displayed. No space is allowed between 1, 2, and>.
- Example:
ls 2> &1> t.txt
The 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> &1
Write 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 figure
ls
Command does not meet the requirements
For more information, see focustc. The blog address is http://blog.csdn.net/caozhk.