The ability to remove comments from various files in a Linux operating system is often used to view a longer file without looking at annotations. Typically these files include *.c written in C, *.h files, CPP files, *.xml files, *.sh shell script files, *.ini *.conf profiles, *.php *.py *.pl files written in programming languages, and executable files with no extensions.
It is not complicated to implement this function, usually there are several types of annotation styles, and you only need to write the appropriate regular expressions and apply the appropriate text processing tools (grep, SED, etc.) in the scripting process.
Writing a script file for several common annotation styles instead of cat is a little more labor-saving.
The script is as follows:
This script can be obtained from GitHub, welcome 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 [email protected] filenamedebug=falseif ${debug} ; then old_ps4= $PS 4 # 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 xtracefifunction 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&nbSP; " ^$ "}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 ;; &nbSp; esac}file=$1if [[ -f ${file} ]]; then del _commentelse echo "ls: cannot access $file: no such File or directory " && exit 1fiif ${DEBUG} ; then export ps4=${old_ps4} ${_xtrace_functions}fi
Tag: delete comments, do not view comments, remove comments
--end--
This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1886595
Linux shell scripts Remove comments from several common types of files