Home Linux Tips & Tricks Print Files Without Comments And Empty Lines In Linux

Print Files Without Comments And Empty Lines In Linux

By sk
Published: Last Updated on 6.7K views

When you display a source code or config file's contents in the standard output using cat command, you will see everything in that file including the commented and empty lines, right? Yes. What if you want to view only the uncommented lines, ignoring all others? Well, it is possible to ignore the commented and empty lines from a file output. In this short guide, we will see how to print files without comments and empty lines in Linux. This can be useful when you want to quickly review a configuration file that contains a lots of comments and empty lines.

What are Comments in computer programming?

If you look into a source code or a configuration file, you will notice a lot of lines starts with either an asterisk "*" or a hash "#" or a slash "/" or a semicolon ";". These lines are known as Comments. In computer programming, a comment is a human-readable description or annotation used to clarify the purpose of the code. They helps the users and other programmers to easily understand what the code is doing. Generally, the comments and empty lines will be ignored by compilers and interpreters. They are only for programmers. The syntax of comments varies in different programming language.

Now let us see how to exclude or skip these comments and empty lines and only display the lines which are not commented.

Print files without comments and empty lines in Linux

Let me show you the contents of the sources.list file of my Ubuntu system:

$ cat /etc/apt/sources.list

Sample output:

# deb cdrom:[Ubuntu 18.04.2 LTS _Bionic Beaver_ - Release amd64 (20190210)]/ bionic main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://it-mirrors.evowise.com/ubuntu/ focal main restricted
# deb-src http://in.archive.ubuntu.com/ubuntu/ bionic main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://it-mirrors.evowise.com/ubuntu/ focal-updates main restricted
# deb-src http://in.archive.ubuntu.com/ubuntu/ bionic-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://it-mirrors.evowise.com/ubuntu/ focal universe
# deb-src http://in.archive.ubuntu.com/ubuntu/ bionic universe
deb http://it-mirrors.evowise.com/ubuntu/ focal-updates universe
# deb-src http://in.archive.ubuntu.com/ubuntu/ bionic-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://it-mirrors.evowise.com/ubuntu/ focal multiverse
# deb-src http://in.archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://it-mirrors.evowise.com/ubuntu/ focal-updates multiverse
# deb-src http://in.archive.ubuntu.com/ubuntu/ bionic-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://it-mirrors.evowise.com/ubuntu/ focal-backports main restricted universe multiverse
# deb-src http://in.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu bionic partner
# deb-src http://archive.canonical.com/ubuntu bionic partner

deb http://it-mirrors.evowise.com/ubuntu/ focal-security main restricted
# deb-src http://security.ubuntu.com/ubuntu bionic-security main restricted
deb http://it-mirrors.evowise.com/ubuntu/ focal-security universe
# deb-src http://security.ubuntu.com/ubuntu bionic-security universe
deb http://it-mirrors.evowise.com/ubuntu/ focal-security multiverse
# deb-src http://security.ubuntu.com/ubuntu bionic-security multiverse

Did you see that many lines are commented out with "#" character? It makes the file less readable. This is just a small file, so it is no big deal. But when you read a really long config files, for example "httpd.conf" or "php.ini", you will have to go through a lots of comments and empty lines and it is a little bit confusing to find which lines are active and which are not.

To filter all comments and empty lines from being displayed in the file output, use grep command like below:

$ grep "^[^#*/;]" /etc/apt/sources.list

Here,

  • the first ^ character indicates the beginning of the line in the given file i.e. /etc/apt/sources.list.
  • [^#*/;] - will display all lines starts with any characters other than these four characters "#", "*", "/", ";". In other words, all lines that starts with characters "#", "*", "/", ";" will be excluded from the output.

Sample output of the above command:

deb http://it-mirrors.evowise.com/ubuntu/ focal main restricted
deb http://it-mirrors.evowise.com/ubuntu/ focal-updates main restricted
deb http://it-mirrors.evowise.com/ubuntu/ focal universe
deb http://it-mirrors.evowise.com/ubuntu/ focal-updates universe
deb http://it-mirrors.evowise.com/ubuntu/ focal multiverse
deb http://it-mirrors.evowise.com/ubuntu/ focal-updates multiverse
deb http://it-mirrors.evowise.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://it-mirrors.evowise.com/ubuntu/ focal-security main restricted
deb http://it-mirrors.evowise.com/ubuntu/ focal-security universe
deb http://it-mirrors.evowise.com/ubuntu/ focal-security multiverse

See? All comments and empty lines are gone. Now the output is quite readable.

Have a look at the following screenshot:

print files without comments and empty lines using grep command in Linux
Print files without comments and empty lines using grep command in Linux

On the left side, you see the file content's with comments and empty lines. On the right, all comments and empty lines are ignored with "grep" command.

I prefer the grep way to filter the unnecessary lines being displayed in output. You can also do it with using awk and sed commands as well.

To print file contents excluding all comments and empty lines with "awk" command, run:

$ awk '$1 ~ /^[^;#]/' /etc/apt/sources.list

Sample output:

Ignore commented and empty lines from file output using awk command
Ignore commented and empty lines from file output using awk command

To display lines starts without comments using "sed" command, run:

$ sed -e '/^#/d' /etc/apt/sources.list

For more usage details, refer the respective command's manual page.

$ man awk
$ man grep
$ man sed

Hope this helps.

You May Also Like

2 comments

Jalal July 1, 2020 - 2:20 pm

Hi,
very useful article…….

Reply
Daniel July 5, 2020 - 12:50 pm

What is the line starts with space(s) followed by a comment-sign?

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More