удалить комментарии, не влияя на значения в файле конфигурации - PullRequest
2 голосов
/ 22 апреля 2020

У меня есть файл конфигурации, и мне нужно удалить комментарии, начиная с # до конца строки. Но это не должно влиять на значения в двойных / одинарных кавычках.

Мой входной файл:

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

Ответы [ 2 ]

3 голосов
/ 22 апреля 2020

Вот скрипт 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> на <> и использовать в файле ...

3 голосов
/ 22 апреля 2020

Не могли бы вы попробовать следующее (написано и протестировано с показанными образцами).

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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...