Use the shell script to count the number of comment lines in the source file. (// , /**/)

Source: Internet
Author: User

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




Related Article

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.