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 [email protected] 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--

时间: 2024-08-24 14:52:09

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

Linux Shell脚本逐行读取多个文件

现有file1.file2.file3三个文件,其内容如下 $cat file1 f1_1 f1_2 f1_3 $cat file2 f2_1 f2_2 f2_3 $cat file3 f3_1 f3_2 f3_3 编写shell脚本逐行读取这三个文件 #!/bin/bash cat file1 file2 file3 |while read p   do     echo $p   done [思考] 采用done引入多个文件,怎么实现? #!/bin/bashwhile read p  do

Linux shell脚本 判断用户输入的文件类型

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 编写一个脚本,从键盘输入一个文件,判断它是否存在,如果存在就判断它是什么类型的文件:并用对应的颜色输出 脚本如下: #!/bin/bash #function:test file type

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

Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Linux 脚本编写基础◆1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编辑好脚本时,如果要执行该脚本,还必须使其可执行. 要使脚本可执

linux shell 脚本获取和替换文件中特定内容

1.从一串字符串中获取特定的信息 要求1:获取本机IP:menu.lst为系统镜象的IP配置文件,需要从中获取到本机IP信息(从文件获取信息) 1 timeout 1 2 default 0 3 4 title live 5 find --set-root /casper/vmlinuz 6 kernel /casper/vmlinuz boot=casper ignore_uuid showmounts ip=eth0,10.0.66.66,255.255.240.0,10.0.64.3 7

《Linux Shell脚本攻略》 笔记 第三章:文件操作

<Linux Shell脚本攻略> 笔记 第三章:文件操作 1.生产任意大小的文件 [[email protected] dd_test]# [[email protected] dd_test]# dd if=/dev/zero of=junk.data bs=1k count=10 10+0 records in 10+0 records out 10240 bytes (10 kB) copied, 0.00137023 s, 7.5 MB/s 2.文件系统相关测试 [ -f $file

Linux shell脚本流程控制

博主搬家至51CTO,初来乍到,请多指教. 此次我们来通过实例解析Linux shell脚本流程控制 Linux shell脚本流程控制可分为三类:顺序执行,条件选择执行,循环执行 顺序执行:简单理解就是逐行执行脚本内容,逐行解读,逐行执行.(此处不做实例解析) 条件选择执行:可以理解为先进行某一条件的判断选择,再决定执行怎样的脚本内容.常见语句if case 条件选择语句:if if语句用法: 单分支 if 判断条件;then 条件为真的分支代码 fi 双分支 if 判断条件; then 条件

LINUX SHELL脚本攻略笔记[速查]

Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述符和重定向 cat 数组和关联数组 alias date 调试脚本 函数和参数 管道 读取命令输出 read 字段分隔符和迭代器 循环 比较和测试 find xargs tr md5sum sha1sum 对目录进行校验 sort uniq tempfile split bash变量匹配切分 exp

Linux Shell脚本攻略(1.4)

1.4 使用函数添加环境变量 1.4.1 简介 环境变量通常用于存储路径列表,这些路径用于搜索可执行文件.库文件等.例如:$PATH.$LD_LIBRARY_PATH,它们通常看起来像这样: PATH=/usr/bin;bin LD_LIBRARY_PATH=/usr/lib;lib 这意味着只要shell需要运行二进制可执行文件时,它会首先查找/usr/bin,然后查找/bin.在ubuntu14.04中,PATH和LD_LIBRARY_PATH存储的路径如下所示: PATH=/usr/loc

Linux Shell脚本例子

Shell脚本是我们运维人员管理的最基础知识,下面就是我在学习过程中的一些小例子(比起大牛来说).写这篇博客的目的,是为了记录自己学习脚本的历程,也是为了能和读者一起探讨学习. # Example1: 自动创建脚本的模板 脚本名:creat_scripts.sh  # 功能描述:creat_scripts.sh SCRIPTS_NAME 如果创建的脚本名文件不存在,则创建成脚本文件; # 如果对应的文件存在,且为脚本文件,则打开文件到最后一行: # 如果对应的文件存在,但不是脚本文件,则提示退出