Today see a help post ask this thing again, so boring wrote a.
With the awk script, which is that the script interpreter uses/usr/bin/awk instead of/bin/sh
But it's all a script, if you want to.
You can call me this awk script with a shell script.
How to: Save the following script as a file like Get-cfile-notes.awk
Then chmod 755 Get-cfile-notes.awk is ready to run.
Attention:
Because there is no good way to use stacks and character processing in this script, you can only handle simple situations. For example, a line appears * * XXXXDFAFW * * * jwo*/* * * and so on.
But as long as you do not deliberately to test my program, the basic use is no problem.
As follows:
#!/usr/bin/awk-f # This procedure is used to count the number of lines in the annotation in the C file # by fdl19881 # 2012.8.10 BEGIN {num_notes = 0; Stat = 0; # 0: No annotations, 1://annotations (notes1), 2:/*/Comments (Notes2)} {if (stat = 0) {notes1 = Match ($,/\/\//); # Find//Position NOTES21 = Mat CH ($,/\/\*/);
# Find/* position if (notes1 > 0 && notes21 = = 0) {#//note stat = 1;
} if (notes1 = = 0 && notes21 = 0) #无注释 stat = 0;
if (notes1 >0 && notes21 >0) {if (Notes1 < NOTES21) stat = 1;
else stat = 2;
} if (notes1 = = 0 && notes21 > 0) stat = 2;
if (stat = = 1) {# At this time//note stat = 0;
Num_notes + 1;
Next
} if (stat = = 2) {#/* Note Notes22 = match ($,/\*\//);
if (Notes22 = = 0) {#没找到 */num_notes + + 1;
Next
} if (Notes21 < notes22) {#/**/comment row num_notes + = 1;
Stat = 0;
Next
else {print "error, find * * * before/*\n" exit;
}} if (stat = 2) {num_notes = 1;
if ($0~/\*\//) {stat = 0; }} end {print Num_notes; }
If you have to use the shell, then you write a shell call it, the simplest way
#!/bin/sh
./get-cfile-notes.awk $
It's okay.
Test:
#include <stdio.h>/* is the My I
/A/haha
int main
AM
Thank you!
/printf ("Hello world!\n");//printf statement return
0;/* return * * * */
}
Save it as Main.c
And then./get-cfile-notes.awk MAIN.C
Output:
7