【发布时间】: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'
【问题讨论】: