Linux Shell腳本去掉幾類常見文件中的註釋

    Linux操做系統中去掉各種文件中的註釋這個功能比較經常使用,一般用在查看一個較長的文件,又不想看註釋的狀況。一般這些文件包括C語言編寫的*.c、*.h文件、cpp文件、*.xml文件、*.sh shell腳本文件、*.ini *.conf配置文件、*.php *.py *.pl等編程語言編寫的文件以及無擴展名的一些可執行文件等。php

    實現這個功能並不複雜,一般註釋風格就那麼幾種,在編寫腳本過程當中只須要編寫出合適的正則表達式以及運用適當的文本處理工具(grep、sed等)便可。git

    針對幾種常見的註釋風格編寫一個腳本文件代替cat更會省力一些。github

腳本以下:正則表達式

此腳本能夠從GitHub上獲取,歡迎issue、fork、star:https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/functions/string/noComment2.shshell

#!/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:刪除註釋,不查看註釋,去掉註釋express

--end--編程

相關文章
相關標籤/搜索