Syntax: WC [options] File ...
Description: This command counts the number of bytes, words, and lines in a given file. If no file name is given, read from standard input. The WC gives also the president count of all specified documents at the same time. A word is the largest string that is separated by a space character area.
The options for this command have the following meanings:
-C Statistics the number of bytes.
-L statistic line count.
-W counts the number of words.
These options can be used in combination.
The order and number of output columns are not affected by the order and number of options.
Always appear in the following order and each item is at most one column.
Number of lines, words, bytes, file names
If there is no file name on the command line, the file name does not appear in the output.
For example:
$ WC-LCW file1 file2
4 file1
7 File2
Total
Example Analysis:
1. Statistics demo directory, JS file number: Find demo/-name "*.js" |wc-l
2. Statistics Demo directory of all JS file code lines: Find demo/-name "*.js" |xargs cat|wc-l or Wc-l ' find./-name "*.js" ' |tail-n1
3. Statistics Demo directory All JS file code line number, filtered empty line: Find/demo-name "*.js" |xargs cat|grep-v ^$|wc-l
Today, during the interview, the interviewer was asked how to use the shell command to get a count of the number of Java code lines in a folder.
Thought for a moment, the basic idea is to find all the Java files under this folder, and then each file statistics code, the outer set for the loop, overlay the results, get the desired results.
But obviously the interviewer wants me to use a shell to get it off the spot and not get it done. Come back to think about it, in fact, it is not too difficult question.
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. But I think I feel a setback. I don't know if there's a better way.
Original link:
Http://www.cnblogs.com/fullhouse/archive/2011/07/17/2108786.html
http://hittyt.iteye.com/blog/1700584