使用shell脚本统计源码文件中的注释行数.(// , /**/)
来源:互联网 发布:淘宝韩国代购真假 编辑:程序博客网 时间:2024/05/18 03:35
今天看到一求助帖子再问这个事,所以无聊写了个。
用的是awk脚本 , 也就是脚本解释器是用/usr/bin/awk , 而不是/bin/sh
但都是脚本 , 如果你想的话,
可以用shell脚本调用我这个awk脚本就行了。
使用方法:将下面的脚本保存成文件如get-cfile-notes.awk
然后chmod 755 get-cfile-notes.awk就可以运行了。
注意:
因为在这种脚本里面没什么好的方法使用栈和逐字符的处理,所以只能处理简单的情况 。 比如一行中出现/* xxxxdfafw */ /* jwo*/ /* */ 等等。
但只要你不是故意要考倒我的这个程序,基本使用是没问题的。
如下:
#!/usr/bin/awk -f# 本程序用来统计c文件中的注释的行数# by fdl19881 # 2012.8.10BEGIN {num_notes = 0;stat = 0; # 0:无注释, 1://注释(notes1) , 2:/* */注释(notes2)}{if(stat == 0) {notes1 = match($0 , /\/\//); # 查找//位置notes21 = match($0 , /\/\*/); # 查找/*位置if(notes1 > 0 && notes21 == 0) { # //注释stat = 1;}if(notes1 == 0 && notes21 == 0) #无注释stat = 0;if(notes1 >0 && notes21 >0) {if(notes1 < notes21)stat = 1;elsestat = 2;}if(notes1 == 0 && notes21 > 0)stat = 2;if(stat == 1) { # 此时为//注释stat = 0;num_notes += 1;next;} if(stat == 2){ # /*注释notes22 = match($0 , /\*\//);if(notes22 == 0) { #没找到*/num_notes += 1;next;}if(notes21 < notes22) { # /**/注释一行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;}
如果你非要用shell的话,那你写个shell调用吧,最简单的方法
#!/bin/sh
./get-cfile-notes.awk $1
就行了
试验:
#include <stdio.h>/* this is my first program */// haha int main(){/* this is my first program thank you !*/printf("hello world!\n"); //printf statementreturn 0; /* return *? */}将其保存为main.c
然后./get-cfile-notes.awk main.c
输出:
7
- 使用shell脚本统计源码文件中的注释行数.(// , /**/)
- shell脚本命令,统计文件行数
- 统计源代码行数Shell脚本
- Shell脚本统计文件行数的8种方法
- Shell脚本统计文件行数的8种方法
- Shell脚本统计文件行数的8种方法
- 统计文件行数的shell
- 统计源代码行数的shell脚本
- 统计源代码行数Shell脚本[00原创]
- 统计代码行数的shell脚本
- 利用shell脚本统计代码行数
- 统计java工程源码行数的脚本
- 利用Shell脚本去掉C/C++源码中的注释
- Linux Shell脚本去掉几类常见文件中的注释
- Shell脚本统计指定目录下子目录中的文件个数
- 求统计文件shell脚本?
- shell学习(一):统计文本行数脚本
- 统计python脚本中 shell命令输出文本的行数
- error prj0019 running moc on "XXXX.h"
- 刷新动画效果
- #pragma pack(n)
- C++中string类的成员函数find_first_not_of()
- 使用 Rails Active Resource 简化 web 应用程序之间的互操作性
- 使用shell脚本统计源码文件中的注释行数.(// , /**/)
- C++中substr函数的用法
- iBatis入门
- Java中的超大数相乘计算
- 548Tree
- POJ 1696 Space Ant (向量的叉积)
- hdu2222 Keywords Search
- VC----文件名字体转换软件
- isnan isinf