删除注释而不影响配置文件中的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除注释而不影响配置文件中的值相关的知识,希望对你有一定的参考价值。
我有一个配置文件,我需要删除从#开始到行末的注释,但它不应该影响双引号中的值。
我的输入文件。
# 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脚本。
#!/usr/bin/perl
use strict;
while (<DATA>){
if (m/^h*#/) {next;};
if (m/((['"])[^2]*2)/) {print substr $_, 0, @+[0]; print "
"; 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>
到 <>
并在文件上使用...
另一答案
请你尝试以下的方法(用所示的例子编写和测试)。
awk '
/^#/{
next
}
/".*"| 47.* 47/{
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.
}
/".*"| 47.* 47/{ ##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.
以上是关于删除注释而不影响配置文件中的值的主要内容,如果未能解决你的问题,请参考以下文章