How to view a file content without comments in Linux ?

Here we will check various ways how to check a file on Linux bash shell by hiding the comments of the file and look for its content only.

Ideally if you see any file on Linux, it shows all contents including comments.

[root@nglinux ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue Nov 14 08:08:22 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=e91618e7-a10b-499a-9652-682de8b5cfa1  /  ext4  defaults,noatime    0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sr0                /mnt/cdrom            iso9660    defaults        0 0
debugfs 		/sys/kernel/debug 	debugfs defaults 0 0
[root@nglinux ~]# 

Now i want to suppress all comments and empty lines from the output.

To achieve this we can use following ways.

1. Remove all comment lines: Using grep command.

[root@nglinux ~]# cat /etc/fstab | grep -v "^#" 

UUID=e91618e7-a10b-499a-9652-682de8b5cfa1  /  ext4  defaults,noatime    0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sr0                /mnt/cdrom            iso9660    defaults        0 0
debugfs 		/sys/kernel/debug 	debugfs defaults 0 0
[root@nglinux ~]# 

2. Remove all comment lines: Using awk command.

[root@nglinux ~]# awk '!/^#/' /etc/fstab

UUID=e91618e7-a10b-499a-9652-682de8b5cfa1  /  ext4  defaults,noatime    0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sr0                /mnt/cdrom            iso9660    defaults        0 0
debugfs 		/sys/kernel/debug 	debugfs defaults 0 0
[root@nglinux ~]# 

Sometimes, we have scenarios when there are too many empty lines also available, for this we can remove empty lines also.

I prefer to use below command to achieve this scenario.

Remove comments and empty lines from output

[root@nglinux ~]# while read line; do if [[ $line != "" ]];then echo $line; fi done < /etc/fstab | grep -v '^#'
UUID=e91618e7-a10b-499a-9652-682de8b5cfa1 / ext4 defaults,noatime 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/sr0 /mnt/cdrom iso9660 defaults 0 0
debugfs /sys/kernel/debug debugfs defaults 0 0
[root@nglinux ~]# 

I hope you liked the trick.

Do post your comments/suggestions below so that it can be useful for others as well.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments