remove comments from file in linux

How to Show File Without Comments in Linux

Often Configuration files of various software and programs contain many lines of comments, making them verbose and large. It can be tedious to go through such a configuration file and find the important settings amidst all those comments. Although you can use grep to quickly find the text you are looking for, it can be refined even further to exclude all the comments from output. This way you will be able to easily view file without comments in Linux. We will look at how to view common configuration files without comments in Linux.

How to Show File Without Comments in Linux

Let us take the example of PHP configuration file /etc/php/7.1/cli/php.ini. In this file, each comment begins with semicolon (;). Here is the command to view the entire php.ini file without comments, using grep.

$ grep ^[^\;] /etc/php/7.1/cli/php.ini

In the above command, we use regular expression with grep to specify that we want to view lines that are not beginning (using ^) with semicolon (;). Since semicolon is a special character, we escape it with backslash(\).

Please note, the above command will display the file content on standard output (terminal). If you want to store it in another file, you can use the redirection operator to write the output to another file. This way you don’t need to run the above command repeatedly.

$ grep ^[^\;] /etc/php/7.1/cli/php.ini > php-no-comment.ini

In some configuration files, the hash (#) character may be used to specify beginning of comments. In such cases, you can use the following command to display file content without comments that start with # character.

$ grep ^[^#] /etc/postfix/

It may also happen that there are whitespaces preceeding semicolon or hash characters in configuration file. In such cases, you can use the following command.

$ egrep -v "^$|^[[:space:]]*;" /etc/php/7.1/cli/php.ini 
$ egrep -v "^$|^[[:space:]]*#" /etc/postfix/

In the above example, -v option means non-matching lines. We further use ^$ for deleting empty spaces. We use ^[[:space:]]# or ^[[:space:]]; to indicate hash or semi colon preceded by a space respectively. We use ‘|’ operator to indicate OR operator for the two regular expressions. Basically, we want to display lines that are not empty or beginning with space followed by hash/semicolon.

In this article, we have learnt how to view file contents in Linux by removing its comments. The key is to tweak the regular expression used for grep or egrep function to match the comments properly. You can also use it within shell scripts to automatically strip comments from a file before processing it further. This can be useful in automated processing of data dumps which may contain comments.

Also read:

How to Enable Screen Sharing in Ubuntu
Linux Show Active Connection On Port
How to Install CSF in CentOS & Ubuntu
How to Install Visual Studio Code in Ubuntu
How to Evaluate Expression in Shell Script

Leave a Reply

Your email address will not be published. Required fields are marked *