Linux Logo

Use grep to eliminate blank lines and comments

A configuration file, specifically, the /etc/samba/smb.conf file contains many lines of comments using both the simi-colon (“;”) and the hash (“#”) as well as many empty lines that are either blank or contain whitespace. Using grep to view the file without these lines is one way to do so. To write the correct command can be a bit more challenging. After some trial-and-error, here is the command.

The grep command that can be used to display the contents of a configuration file without comment, whitespace lines, and blank lines is this:

grep -v '^;\|#\|^\s$\|^$' /etc/samba/smb.conf

Some explanation to this somewhat cryptic command:

  • The -v is used to select non-matching lines, or could mean exclude these lines from the final output.
  • The ^ in front of each pattern means select from the beginning of the line.
  • The \ is an escape needed for the | pipe or the pipe will be read as part of the pattern.
  • The \s (??) (Note: Sourced below)

This command was tested on a CentOS Linux 6.5 (64-bit) install.

Source(s)
http://ubuntuforums.org/showthread.php?t=1725917