首页 > 代码库 > Linux Shell脚本去掉几类常见文件中的注释
Linux Shell脚本去掉几类常见文件中的注释
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
#!/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--
本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1886595
Linux Shell脚本去掉几类常见文件中的注释
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。