Bash script that removes C source comments

时间:2015-06-15 14:23:38

标签: bash

I need to write a bash script which copies all .c files from a directory $2 to another directory $1, but without comments.

I only have to remove comments that begin with //, might have tabs/spaces before the comment, but not letters.

Also, I need to do it with only a few executed lines (no workaround using ;). Does anyone has an idea?

For now I have the .c files stored in an array which I need to iterate through:

file_list=()
while IFS= read -d $'\0' -r file
do
    file_list=("${file_list[@]}" "$file")
done < <(find "$2" -maxdepth 1 -name '*.c' -print0)

Thanks

1 个答案:

答案 0 :(得分:0)

通过sed命令传递所有文件,如下所示:

sed -i "s#[[:space:]]*//.*##g" filepath

如果您想保留其前面有代码的评论(例如i++;//comment),那么:

sed -i "/^[[:space:]]*\/\/.*/d" filepath