删除C++代码注释的脚本
2009-08-08 00:34
1271 查看
#!/bin/sh -f function del_comment_file() { #C++模式注释的删除。 #Delete the line begins with // #首先删除//在行首的行 sed -i '/^[ /t]*/////d' $file #Delete the line ends with // #注意已经除去了 //在行首的情况,下面匹配除去URL的//部分,因为代码中有一部分中有 #URL,形如fun("ftp://") sed -i 's/////[^"]*//' $file #删除以C语言中注释只在一行的行 sed -i 's////*.*/*////' $file #Delete the lines between /* and */ #删除C语言中注释跨行的情况 sed -i '/^[ /t]*///*/,/.*/*///d' $file } function del_comment() { #$Dir=`pwd` for file in `ls`;do case $file in *.c ) del_comment_file ;; *.cpp ) del_comment_file ;; *.h ) del_comment_file ;; * ) if [ -d $file ];then cd $file del_comment cd .. fi ;; esac done } Dir=$1 if [ ! -e $Dir ];then echo "The Directory isn't exist." exit fi cd $Dir del_comment
直接给脚本一个目录,它就会把目录下的C++文件的注释删除。
该文件用来终极删除注释,操作系统限linux
#!/bin/bash
###the script use to delete comment
if [ -z "$1" ];then #检测是否输入了源文件
echo "ipput the source file"
exit 1
fi
postfix=`echo $1 |cut -f 2 -d '.'` #取文件的后缀,以便根据不同的文件进行不同的处理
if [ -n "$2" ];then #存放处理源文件产生的信息的文件
target_file=$2 #如果给出了就直接调用
touch ${target_file}
else
prefix=`echo $1|cut -f 1 -d '.' ` #如果没有给出的话则生成一个和源文件名字一样,但后缀为temp的文件
target_file=${prefix}.temp
touch ${target_file}
fi
case "postfix" in
sh )
echo "it is shell script !"
sed 's/[[:space:]]#.*//g' $1 |sed '/^#/d'|/
sed '/^[[:space:]]*$/d' | sed '/^$/d' >${target_file}
echo "the source file is $1,target file is ${target_file}"
;;
c|java)
echo "c or java program"
sed 's////*.*/*////g' $1|sed '////*/,/.*/*///d' |/
sed 's/////.*//g' |/
sed '/^[[:space:]]*$/d' |sed '/^$/d' >${target_file}
echo "the source file is $1,target file is ${target_file}"
;;
*)
echo "unknown file type !"
rm -f ${target_file}
;;
esac
//-------------------------
#!/bin/sh -f
function del_comment_file()
{
#C++模式注释的删除。
#Delete the line begins with //
#首先删除//在行首的行
sed -i '/^[ /t]*/////d' $file
#Delete the line ends with //
#注意已经除去了 //在行首的情况,下面匹配除去URL的//部分,因为代码中有一部分中有
#URL,形如fun("ftp://")
sed -i 's/////[^"]*//' $file
#删除以C语言中注释只在一行的行
sed -i 's////*.*/*////' $file
#Delete the lines between /* and */
#删除C语言中注释跨行的情况
sed -i '/^[ /t]*///*/,/.*/*///d' $file
}
function del_comment()
{
#$Dir=`pwd`
for file in `ls`;do
case $file in
*.c )
del_comment_file
;;
*.cpp )
del_comment_file
;;
*.h )
del_comment_file
;;
* )
if [ -d $file ];then
cd $file
del_comment
cd ..
fi
;;
esac
done
}
Dir=$1
if [ ! -e $Dir ];then
echo "The Directory isn't exist."
exit
fi
cd $Dir
del_comment
相关文章推荐
- 怎样删除C/C++代码中的所有注释?浅谈状态机的编程思想
- 将C/C++代码中的注释删除
- 怎样删除C++代码中的注释——有限状态机思想的使用
- 将C/C++代码中的注释删除
- 状态机编程思想(2):删除代码注释(目前支持C/C++和Java)
- 状态机编程思想(2):删除代码注释(目前支持C/C++和Java)
- 怎样删除C/C++代码中的所有注释?浅谈状态机的编程思想
- Java 删除c/c++/java代码注释的代码片段
- C++语言实现代码注释的自动删除
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 删除指定字符c++代码实例及运行结果
- 指定几个目录,有选择的对C/C++/JAVA等类型的代码,进行grep查找字符串的脚本
- 修改C/C++的单行注释(//)为多行注释(/**/)的Perl代码
- (如何在C++代码中调用Lua脚本的函数?)
- 经验分享之SVN禁止删除和强制注释操作的钩子脚本pre-commit
- 在C++代码中调用L脚本语言
- 用来删除菊花公司代码中大量无用的函数头部注释的python剃刀
- 去掉C/C++程序代码中的注释
- 删除C/C++中的注释-有限状态机(C语言实现)
- 使用VAssistX为VS2008 c++代码添加函数头注释http://blog.sina.com.cn/s/blog_4aff4b970101bfqs.html