Most Linux and UNIX-like system configuration files are documented using comments, but some time I just need to see line of configuration text in a config file. How can I view just the uncommented configuration file directives from squid.conf or httpd.conf file? How can I strip out comments and blank lines on a Linux or Unix-like systems?

To view just the uncommented lines of text in a config file
Tutorial details
Difficulty level Easy
Root privileges No
Requirements grep/egrep/sed
Est. reading time 1m
use the grep, sed, awk, perl or any other text processing utility provided by UNIX / BSD / OS X / Linux operating systems.

grep command example to strip out data

You can use the gerp command as follows:
$ grep -v "^#" /path/to/config/file
$ grep -v "^#" /etc/apache2/apache2.conf

Sample outputs:

ServerRoot "/etc/apache2"
 
LockFile /var/lock/apache2/accept.lock
 
PidFile ${APACHE_PID_FILE}
 
Timeout 300
 
KeepAlive On
 
MaxKeepAliveRequests 100
 
KeepAliveTimeout 15
 
 
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>
 
<IfModule mpm_worker_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>
 
<IfModule mpm_event_module>
    StartServers          2
    MaxClients          150
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxRequestsPerChild   0
</IfModule>
 
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
 
 
AccessFileName .htaccess
 
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>
 
DefaultType text/plain
 
 
HostnameLookups Off
 
ErrorLog /var/log/apache2/error.log
 
LogLevel warn
 
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
 
Include /etc/apache2/httpd.conf
 
Include /etc/apache2/ports.conf
 
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
 
CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined
 
 
 
Include /etc/apache2/conf.d/
 
Include /etc/apache2/sites-enabled/

To suppress blank lines use egrep command, run:

egrep -v "^#|^$" /etc/apache2/apache2.conf
## or pass it to the page such as more or less ##
egrep -v "^#|^$" /etc/apache2/apache2.conf | less
 
## Bash function ######################################
## or create function or alias and use it as follows ##
## viewconfig /etc/squid/squid.conf                  ##
#######################################################
viewconfig(){
   local f="$1"
   [ -f "$1" ] && command egrep -v "^#|^$" "$f" || echo "Error $1 file not found."
}

Sample output:

Fig.01: Unix/Linux Egrep Strip Out Comments Blank Lines

Understanding grep/egrep command line options

The -v option invert the sense of matching, to select non-matching lines. This option should work under all posix based systems. The regex ^$ matches and removes all blank lines and ^# matches and removes all comments that starts with a “#”.

sed Command example

GNU / sed command can be used as follows:
$ sed '/ *#/d; /^ *$/d' /path/to/file
$ sed '/ *#/d; /^ *$/d' /etc/apache2/apache2.conf

GNU or BSD sed can update your config file too. The syntax is as follows to edit files in-place, saving backups with the specified extension such as .bak:

sed -i'.bak.2015.12.27' '/ *#/d; /^ *$/d' /etc/apache2/apache2.conf

For more info see man pages – sed(1)

This entry is 7 of 7 in the Linux / UNIX grep Command Tutorial series. Keep reading the rest of the series:
  1. How To Use grep Command In Linux / UNIX
  2. Regular Expressions In grep
  3. Search Multiple Words / String Pattern Using grep Command
  4. Grep Count Lines If a String / Word Matches
  5. Grep From Files and Display the File Name
  6. How To Find Files by Content Under UNIX
  7. grep command: View Only Configuration File Directives

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 8 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • Jeff Schroeder May 29, 2008 @ 14:11

    Thats a trick I’ve been using for years, but have came up with a better way to do it than you:

    egrep -v '^#|^$' /etc/httpd/conf/httpd.conf

    Your sed line does the exact same thing in removing empty lines. Egrep is more concise.

    • sss Dec 31, 2010 @ 11:08

      # egrep -v ‘^#|^$’ dovecot.conf
      -su: ^$’: command not found

      not quite foolproof :)

  • Raj May 29, 2008 @ 14:42

    Same trick here but with cat -s

    grep -v "^#" httpd.conf | cat -s

    Indeed, jeff has demonstrated a better way.

  • Jeff Schroeder Dec 31, 2010 @ 15:18

    @sss: Clearly… you aren’t typing the command the same as I did. Why? Because the pipe is being interpreted by the shell and is trying to run it. That tells me that you didn’t actually put it in the single quotes aka ‘ ‘ like your command says. If you did, the shell would not be able to interpret it. Well unless your shell is something really really buggy and I don’t think thats the case as too much would break.

  • Philip Oct 14, 2011 @ 18:36

    Wow, Jeff coming back literally years later to correct sss, hah

    • Guy Incognito Jul 18, 2013 @ 15:31

      Look closer.
      sss posted his reply to Jeff’s original comment years after the OP was made. Jeff responded to sss a matter of hours after sss made his reply.

      Who me? I’m just passing by… =P

  • ntk Jan 28, 2012 @ 15:27

    hi, is there a table comparing the syntax between regex in sed and grep? Otherwise could you help me to write in sed the command, check a string, that it should contains cafe, it should have no more then 6 alpha characters and not 3 immediate repeating of the number, or char like cafewaterloo, is ok, caffe111waterloo is not allowed, but cafe 101 waterloo is ok for example

  • jesusguevarautomotriz Sep 8, 2016 @ 5:48

    With Vim

    Only view Uncommented Lines, include blank lines
    :g!/^#

    alternatively, and/or: Only view and supress blank lines
    :g!/^#\|^$

    alternatively, and/or:
    To copy into register a (“some vim clipboard”), first delete register a
    qaq

    and then, make search and that append into register a
    g!/^#\|^$/y A

    Open new buffer, paste search result
    "aP

    Tested gVim 7.4 only.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum