【问题标题】:remove comments without affecting values in the config file删除注释而不影响配置文件中的值
【发布时间】:2020-08-05 17:18:39
【问题描述】:

我有一个配置文件,我需要删除以 # 开头到行尾的 cmets。 但它不应该影响双引号/单引号中的值。

我的输入文件:

# comment1
# comment2
#hbase_table_name=mytable # hbase table.
hbase_table_name=newtable # hbase table.
hbase_txn_family=txn
app_name= "cust#100"  # Name of the application
app_user= 'all#50,all2#100'  # users
hbase.zookeeper.quorum=localhost
zookeeper.znode.parent=/hbase-secure
hbase.zookeeper.property.clientPort=2181

我正在尝试的 perl 命令

perl -0777 -pe ' s/^\s*$//gms ; s/#.*?$//gm; s/^\s*$//gms;s/^$//gm' config.txt

我得到的输出是

hbase_table_name=newtable
hbase_txn_family=txn
app_name= "cust
app_user= 'all
hbase.zookeeper.quorum=localhost
zookeeper.znode.parent=/hbase-secure
hbase.zookeeper.property.clientPort=2181

但是需要的输出是

hbase_table_name=newtable
hbase_txn_family=txn
app_name= "cust#100"
app_user= 'all#50,all2#100'
hbase.zookeeper.quorum=localhost
zookeeper.znode.parent=/hbase-secure
hbase.zookeeper.property.clientPort=2181

我正在寻找使用任何工具的 bash 解决方案 - awk 或 perl 可以解决这个问题。

一种罕见的情况可能是像这样的配置条目

app_user= 'all#50,all2#100'  # users - "all" of them

结果应该是app_user= 'all#50,all2#100'

【问题讨论】:

    标签: perl awk


    【解决方案1】:

    这是一个 perl 脚本:

    #!/usr/bin/perl
    
    use strict;
    
    while (<DATA>){
        if (m/^\h*#/) {next;};
        if (m/((['"])[^\2]*\2)/) {print substr $_, 0, @+[0]; print "\n"; next; }
        s/#.*$//; print ;
    }
    
    __DATA__
    # comment1
    # comment2
    #hbase_table_name=mytable # hbase table.
    hbase_table_name=newtable # hbase table.
    hbase_txn_family=txn
    app_name= "cust#100"  # Name of the application
    #app_name= "cust#100"  # Name of the application
    app_user= 'all#50,all2#100'  # users
    hbase.zookeeper.quorum=localhost
    zookeeper.znode.parent=/hbase-secure
    hbase.zookeeper.property.clientPort=2181
    # from comments, other lines
    hbase_table_name=newtable ## hbase table.
    app_user= 'all#50,all2#100'  # users - "all" of them
    

    输出:

    hbase_table_name=newtable 
    hbase_txn_family=txn
    app_name= "cust#100"
    app_user= 'all#50,all2#100'
    hbase.zookeeper.quorum=localhost
    zookeeper.znode.parent=/hbase-secure
    hbase.zookeeper.property.clientPort=2181
    hbase_table_name=newtable 
    app_user= 'all#50,all2#100'
    

    &lt;DATA&gt; 更改为&lt;&gt; 并用于文件...

    【讨论】:

    • 是的,它很好用.. \h 和 \s 之间有什么区别..
    • @stack0114106: \h 和 \s 之间有什么区别 在大多数正则表达式风格中,\s 包括行终止符,因此 /ABC\s+DEF/ 匹配 ABC DEF 并匹配 @ 987654328@。它相当于 POSIX [[:space:]] 字符类,包括 [ \t\v\r\n\f]。正则表达式 /ABC\h+DEF/ 仅匹配水平空格(不匹配换行符,仅匹配 [ \t] ,相当于 POSIX [[:blank:]] 字符类)。如果可用,请将其用于您只打算用于单行的匹配。
    • 在这种情况下,有点学术,因为我们正在逐行读取文件。如果你用0777 吞下文件,那不是学术性的!
    • 非常感谢您的 \h 参考,不知道这些年来我是怎么错过的。
    【解决方案2】:

    您能否尝试以下操作(使用所示示例编写和测试)。

    awk '
    /^#/{
      next
    }
    /".*"|\047.*\047/{
      match($0,/.*#/)
      print substr($0,RSTART,RLENGTH-1)
      next
    }
    {
      sub(/#.*/,"")
    }
    1
    '  Input_file
    

    说明:为上述代码添加详细说明。

    awk '                                   ##Starting awk program from here.
    /^#/{                                   ##Checking condition if a line starts from #  then do following.
      next                                  ##next will skip all further statements from here.
    }
    /".*"|\047.*\047/{                      ##Checking condition if a line matching regex from " to * OR single quote to single quote in current line.
      match($0,/.*#/)                       ##If above TRUE then come inside block; using match to match everything till # here.
      print substr($0,RSTART,RLENGTH-1)     ##Printing substring which prints from starting to length of matched regex with -1 to remove # in it.
      next                                  ##next willskip all further statements from here.
    }
    {
      sub(/#.*/,"")                         ##This statement will executewhen either a line is NOT starting from # OR  does not have single/double quote in it.
    }
    1                                       ##1 will print edited/non-edited lines here.
    

    【讨论】:

    • 你可以试试hbase_table_name=newtable ## hbase table.
    • @stack0114106,是的,它提供了价值,直到 hbase_table_name=newtable 看起来不错恕我直言,或者我在这里错过了什么。
    • 它没有涵盖我提到的罕见场景。顺便说一句,看起来不错。
    • @stack0114106,我刚刚检查了您罕见的行,它是app_user= 'all#50,all2#100',对我来说看起来不错。请让我知道我们在这里是否很好。
    • @stack0114106,我现在从框中退出,但看起来我的版本也在附近,但 GNU awk 只有你测试并编写了它。
    猜你喜欢
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2015-05-11
    相关资源
    最近更新 更多