Comments and blank lines can be great. They provide context and can make configuration files easier to read. So why in the world would you want to get rid of them? Well, to aid in troubleshooting!
Can you imagine paging through a 1,200 line configuration file looking for anything that might be out of place? We all know that comments do not influence the running configuration, so weed them out! Blank lines don’t do anything either. Be gone!
What you’re left with is the exact configuration that’s in use. Now start looking for typos. Now start looking at parameters that might influence the behavior of the application that you’re troubleshooting. Enough of the why — here’s the how.
Use the following grep command to strip out comments and blank lines:
$ grep -Ev "^#|^$" file
The -E options enables extended regular expressions. This allows us to use the pipe to represent the “or” condition in our pattern. The -v option inverts the match meaning that grep will only print lines that do not match our search pattern. The “^#” pattern matches all lines that begin with a pound sign (#) while the “^$” pattern matches all the blank lines.
[jason@linuxsvr etc]$ head /etc/sysctl.conf # Kernel sysctl configuration file for Red Hat Linux # # For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and # sysctl.conf(5) for more details. # Controls IP packet forwarding net.ipv4.ip_forward = 0 # Controls source route verification net.ipv4.conf.default.rp_filter = 1 [jason@linuxsvr etc]$ grep -Ev '^#|^$' sysctl.conf net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 net.bridge.bridge-nf-call-ip6tables = 0 net.bridge.bridge-nf-call-iptables = 0 net.bridge.bridge-nf-call-arptables = 0 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 4294967295 kernel.shmall = 268435456 [jason@linuxsvr etc]$
For more tips and tricks like this, check out Command Line Kung Fu.