Linux操作系统中去掉各类文件中的注释这个功能比较常用,通常用在查看一个较长的文件,又不想看注释的情况。通常这些文件包括C语言编写的*.c、*.h文件、cpp文件、*.xml文件、*.sh shell脚本文件、*.ini *.conf配置文件、*.php *.py *.pl等编程语言编写的文件以及无扩展名的一些可执行文件等。

    实现这个功能并不复杂,通常注释风格就那么几种,在编写脚本过程中只需要编写出合适的正则表达式以及运用适当的文本处理工具(grep、sed等)即可。

    针对几种常见的注释风格编写一个脚本文件代替cat更会省力一些。

脚本如下:

此脚本可以从GitHub上获取,欢迎issue、fork、star:https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/functions/string/noComment2.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# delete all spaces and comments of specialized file, using with $@ filename
 
DEBUG= false
 
if  ${DEBUG} ;  then
     old_PS4=$PS4   # system builtin variable does not need '${var}' expression
#    export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '
     export  PS4= '+${LINENO}: ${FUNCNAME[0]}: '  # if there is only one bash script, do not display ${BASH_SOURCE}
     _XTRACE_FUNCTIONS=$( set  +o |  grep  xtrace)
     set  -o xtrace
fi
 
function  is_file_exist(){
     test  -f $1 ||  echo  "ls: cannot access $file: No such file or directory"  &&  exit  1
}
 
function  dos2unix_text_file_format_converter(){
     if  cat  -A ${ file } |  grep  '\^M\\$'  > /dev/null  ||  file  ${ file } |  grep  "with CRLF line terminators"  > /dev/null  then
         which  dos2unix > /dev/null  2>&1 || yum -q -y  install  dos2unix || apt-get -qq -y  install  dos2unix
         dos2unix ${ file } > /dev/null
     fi
}
 
function  del_comment_in_c_cpp_file(){
     tmp_file= /tmp/ .noComment_$( date  +%Y%m%d%H%M%S%N$RANDOM)
     cp  ${ file } ${tmp_file}
 
     #delete the comment line begin with '//comment'
     sed  -i  "/^[ \t]*\/\//d"  ${tmp_file}
 
     #delete the comment line end with '//comment'
     sed  -i  "s/\/\/[^\"]*//"  ${tmp_file}
 
     #delete the comment only occupied one line '/* comment */'
     sed  -i  "s/\/\*.*\*\///"  ${tmp_file}
 
     #delete the comment that occupied many lines '/*comment
     #                                              *comment
     #                                              */
     sed  -i  "/^[ \t]*\/\*/,/.*\*\//d"  ${tmp_file}
 
     grep  - v  ^$ ${tmp_file}
 
     \ rm  -f ${tmp_file}
}
 
function  del_comment_in_sh_conf_file(){
     #ignore the comment line end with '# comment'
     grep  - v  "^[ \t]*\#"  ${ file } |  grep  - v  "^$"
}
 
function  del_comment_in_xml_file(){
     if  test  -f ${ file } &&  file  ${ file } |  grep  "XML"  > /dev/null then
         which  tidy > /dev/null  2>&1 || yum -q -y  install  tidy > /dev/null  2>&1 || apt-get -qq -y  install  tidy > /dev/null  2>&1
         tidy -quiet -asxml -xml -indent -wrap 1024 --hide-comments 1 ${ file }
     else
         which  tidy > /dev/null  2>&1 || yum -q -y  install  tidy > /dev/null  2>&1 || apt-get -qq -y  install  tidy > /dev/null  2>&1
         tidy -quiet -asxml -xml -indent -wrap 1024 --hide-comments 1 ${ file }
     fi
}
 
function  del_comment_in_general_file(){
     #ignore the comment line end with '# comment'
     grep  - v  "^[ \t]*\#"  ${ file } |  grep  - v  "^[ \t]*\;"  | grep  - v  "^$"
}
 
 
function  del_comment(){
     case  ${ file in
         *.c|*.cpp|*.h)
             del_comment_in_c_cpp_file
             ;;
         *.sh|*.conf)
             del_comment_in_sh_conf_file
             ;;
         *.xml)
             del_comment_in_xml_file
             ;;
         *)
             del_comment_in_general_file
             ;;
     esac
}
 
file =$1
if  [[ -f ${ file } ]];  then
     del_comment
else
     echo  "ls: cannot access $file: No such file or directory"  &&  exit  1
fi
 
if  ${DEBUG} ;  then
     export  PS4=${old_PS4}
     ${_XTRACE_FUNCTIONS}
fi

tag:删除注释,不查看注释,去掉注释

--end--





本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1886595 ,如需转载请自行联系原作者