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/nullthen
        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 ${filein
        *.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,如需转载请自行联系原作者

Linux Shell脚本去掉几类常见文件中的注释相关推荐

  1. linux shell脚本自动批量解压文件

    单个文件解压很简单,批量不确定目录的压缩包呢?解压到原路径?解压后删除原压缩包?本脚本可一键解决以上所有问题 linux shell脚本自动批量解压文件 脚本免费下载地址: 传送门https://do ...

  2. linux shell脚本 删除指定目录下文件夹(可指定文件夹名、时间)

    情景:需要删除以201812开头的.6天前修改的文件夹(文件夹里包含文件).鼓捣了好一会,开始用find /home/users/niu/test/log/ -name '201812*' -type ...

  3. Linux shell脚本基础学习详细介绍(完整版)一

    Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提. 1. Li ...

  4. Linux shell脚本基础学习详细介绍(完整版)

    Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提. 1. Li ...

  5. linux连接oracle的日志,linux shell脚本连接oracle查询数据插入文件和日志文件中

    #!/bin/sh sqlplus "用户名/密码@数据库"< sqlplus "用户名/密码"< sqlplus -S "用户名/密码& ...

  6. linux 检查权限,检查目录下 文件的权限-linux shell脚本,

    检查目录下 文件的权限-linux shell脚本, #!/bin/bash #History: #2019/07/23    Fsq #This Program will check Permiss ...

  7. linux配置定时删除日志文件,Linux使用shell脚本定时删除历史日志文件

    Linux使用shell脚本定时删除历史日志文件,文件,小时,时间,目录,脚本 Linux使用shell脚本定时删除历史日志文件 易采站长站,站长之家为您整理了Linux使用shell脚本定时删除历史 ...

  8. linux脚本定时拷贝文件,使用Linux shell脚本实现FTP定时执行批量下载指定文件

    使用FTP定时批量下载指定文件的shell脚本,具体实例介绍如下所示: 1.目标FTP服务器地址 #FTP服务器地址 ip=10.19.15.23 2.FTP账号和密码 u=账号 p=密码 3.使用m ...

  9. Linux shell脚本执行后出现语法错误: 未预期的文件结尾

    Windows环境下编写了一个shell脚本,上传到Linux环境中执行,Linux shell脚本执行后出现 语法错误: 未预期的文件结尾. 出现了此错误提示,进行了如下的检查:1.检查Shell脚 ...

最新文章

  1. Bug改到怀疑人生…… | 每日趣闻
  2. 如何定义一个不能被继承的类
  3. SDWebImage 在Swift中遇坑解决
  4. spring aop实现过程之三Spring AOP中Aspect编织的实现
  5. bash-shell详解
  6. Feature Pyramid Networks for Object Detection 论文笔记
  7. EXCEL数据有效性—单元格筛选的改进
  8. 数据挖掘之apriori算法(python实现详细注释)
  9. 什么是取整?有几种取整方式?C语言又是哪种方式?取模取余一样吗?
  10. 关于Spring MVC 文件大小超过限制后浏览器无响应解决方案
  11. android型号手机怎么截图,如何在Android手机上屏幕截图(仅限Rooted Phone) | MOS86...
  12. 香港十大炒黄金交易公司排名2020版一览
  13. NB-IOT与物联网
  14. MD编辑器就是不告诉你之表情
  15. 云计算除了具有极高的市场效益外,简化企业IT运营、内置安全和易于部署等优势非常明显
  16. 移动端SEO优化需要怎么做排名?
  17. rp软件app流程图_app开发流程图_app的制作流程图
  18. 测试cpu调度软件,性能测试分析之CPU篇
  19. 【读书笔记】《用一年时间重生》-第二章
  20. 计算机休眠后无法启动,电脑休眠后硬盘无法启动怎么办【解决方法】

热门文章

  1. GDCM:gdcm::PhotometricInterpretation的测试程序
  2. Boost:验证atomic <>没有对void指针提供算术运算
  3. DCMTK:图像服务器中央测试节点(ctn)主程序
  4. DCMTK:使用dcmimage库将DICOM图像转换为PPM或PGM
  5. OpenCV添加(混合)两个图像
  6. C语言二叉搜索树返回key的树级(附完整源码)
  7. C语言通过链表指针删除链表节点的算法(附完整源码)
  8. C语言链表是否为循环表的算法(附完整源码)
  9. c++Selection Sort选择排序的实现算法(附完整源码)
  10. C语言递归算法将十进制转换为二进制(附完整源码)