Remove Comments and Whitespace from Command Output
Often a Linux distribution or software package will add extensive and informative comments to a configuration file. One example of this is the Postfix main.cf file
While the comments can be invaluable, sometimes you want just the basic file without any extras.
To turn a heavily-annotated file into a bare file, you will need to remove 2 sets of lines:
- Any line that begins with a comment symbol (That symbol varies between different applications or programming languages, but it's usually the Hash symbol)
- Any line that is only whitespace.
This is straightforward with the sed
command:
cat /etc/postfix/main.cf | sed -e '/^#/d' -e '/^$/d'
-e
simply means “this is a sed command” and is a good way of including multiple commands/searches/etc. That means you don't have to try to merge multiple searches into a single command./search/d
means “if the search defined in search matches, delete (d
) that line from the output.- The
^#
search means “match lines that have a hashtag at the beginning of the line.” - The
^$
search means “match lines that have nothing between the start of the line and the end of the line.
Questions? hit me up on Mastodon at @anthonyclarka2@bsd.network.