Linux Shell脚本去掉几类常见文件中的注释
来源:互联网 发布:webpackconfig.js 编辑:程序博客网 时间:2024/05/14 03:33
Linux操作系统中去掉各类文件中的注释这个功能比较常用,通常用在查看一个较长的文件,又不想看注释的情况。通常这些文件包括C语言编写的*.c、*.h文件、cpp文件、*.xml文件、*.sh shell脚本文件、*.ini *.conf配置文件、*.php *.py *.pl等编程语言编写的文件以及无扩展名的一些可执行文件等。
实现这个功能并不复杂,通常注释风格就那么几种,在编写脚本过程中只需要编写出合适的正则表达式以及运用适当的文本处理工具(grep、sed等)即可。
针对几种常见的注释风格编写一个脚本文件代替cat更会省力一些。
脚本如下:
此脚本可以从GitHub上获取,欢迎issue、fork、star:/DingGuodong/LinuxBashShellScriptForOps/blob/master/functions/string/
#!/bin/bash# delete all spaces and comments of specialized file, using with $@ filenameDEBUG=falseif ${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 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 "^$"}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=$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:删除注释,不查看注释,去掉注释
--end--
0 0
- Linux Shell脚本去掉几类常见文件中的注释
- 利用Shell脚本去掉C/C++源码中的注释
- Linux SHELL脚本 注释
- 用shell脚本去掉源码的注释
- sed 去掉文件中的注释#
- 使用shell脚本统计源码文件中的注释行数.(// , /**/)
- shell脚本删除linux中的文件
- linux中用shell去掉源码注释
- Shell去掉文件中的空行
- 删除文件中的空行和注释行 linux shell
- 【Shell脚本学习4】几种常见的Shell
- linux使用脚本shell批量替换文件中的文本
- shell去掉文件中的特殊字符
- Linux几种常见shell简介
- linux shell脚本中的延时
- linux shell脚本中的$,$*,$@和$#
- Linux中的shell脚本浅析
- Linux中的shell脚本编程
- 【关于选择】—-(1)放下努力和坚持吧
- 168. Excel Sheet Column Title
- angular js 17年1月小结 (包括简单的指令和模块 服务等)
- 从web.xml入手分析jeecms配置文件
- 小数点位数的处理
- Linux Shell脚本去掉几类常见文件中的注释
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming! 哈希
- A Knight's Journey
- ios image not found
- Php基础语法
- Linu学习笔记之——ubuntu安装、卸载JDK1.7
- Spark的一些配置总结
- elasticsearch入门教程二(基于restful 针对5.1版本)
- 对不起,给不了你太多