Remover espaços em branco e comentários de arquivos no Linux
Introdução
Muitas vezes nos deparamos com um arquivo de configuração repleto de comentários, não que não sejam importantes mas para simplificar a leitura ou exemplificar algo, queremos apenas o que é necessário. Para isso podemos rodar o seguinte comando no bash que fará a mágica:
# cat <arquivo> |grep -v "#" |awk 'NF>0'
Um exemplo abaixo do resultado esperado:
Peguei um arquivo de exemplo o chrony.conf:
# Welcome to the chrony configuration file. See chrony.conf(5) for more # information about usable directives. # Include configuration files found in /etc/chrony/conf.d. confdir /etc/chrony/conf.d # Use Debian vendor zone. #pool 2.debian.pool.ntp.org iburst server a.st1.ntp.br iburst nts server b.st1.ntp.br iburst nts server c.st1.ntp.br iburst nts server d.st1.ntp.br iburst nts # Use time sources from DHCP. sourcedir /run/chrony-dhcp # Use NTP sources found in /etc/chrony/sources.d. sourcedir /etc/chrony/sources.d # This directive specify the location of the file containing ID/key pairs for # NTP authentication. keyfile /etc/chrony/chrony.keys # This directive specify the file into which chronyd will store the rate # information. driftfile /var/lib/chrony/chrony.drift # Save NTS keys and cookies. ntsdumpdir /var/lib/chrony # Uncomment the following line to turn logging on. #log tracking measurements statistics # Log files location. logdir /var/log/chrony # Stop bad estimates upsetting machine clock. maxupdateskew 100.0 # This directive enables kernel synchronisation (every 11 minutes) of the # real-time clock. Note that it can’t be used along with the 'rtcfile' directive. rtcsync # Step the system clock instead of slewing it if the adjustment is larger than # one second, but only in the first three clock updates. makestep 1 3 # Get TAI-UTC offset and leap seconds from the system tz database. # This directive must be commented out when using time sources serving # leap-smeared time. leapsectz right/UTC
Agora rodando o comando abaixo, teremos o seguinte resultado:
cat chrony.conf |grep -v "#" |awk 'NF>0'
confdir /etc/chrony/conf.d server a.st1.ntp.br iburst nts server b.st1.ntp.br iburst nts server c.st1.ntp.br iburst nts server d.st1.ntp.br iburst nts sourcedir /run/chrony-dhcp sourcedir /etc/chrony/sources.d keyfile /etc/chrony/chrony.keys driftfile /var/lib/chrony/chrony.drift ntsdumpdir /var/lib/chrony logdir /var/log/chrony maxupdateskew 100.0 rtcsync makestep 1 3 leapsectz right/UTC