删除注释而不影响配置文件中的值

问题描述 投票:0回答:1

我有一个配置文件,我需要删除从#开始到行末的注释,但它不应该影响双引号中的值。

我的输入文件。

# 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'

bash perl awk
1个回答
4
投票

这是一个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'

改变 <DATA><> 并在文件上使用...


4
投票

请你尝试以下的方法(用所示的例子编写和测试)。

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.
© www.soinside.com 2019 - 2024. All rights reserved.