Friday 28 January 2011

Removing comments and blank lines out of files

Sometimes I have the need to read configuration files, but I do not want to see the comments in them, or the blank lines - I just want to see the information that is relevant. If you have ever had to deal with a "default" configuration file for the Apache Web Server, or the Squid Proxy Server, then you know what I mean. The default files will usually have big sections of comments, to be used as examples or reminders of features that you can switch on or off.

I find this next line useful in removing comments and blank lines:

grep -v ^# file_name|grep -v '^[[:space:]]*$'

For example, if the file is called web.txt and result of running the line over the file is:

[ ric_man @ mymachine ~/test ] 506 $ cat web.txt
### Section 1: Global Environment
#
# The directives in this section affect the overall operation of Apache,
# such as the number of concurrent requests it can handle or where it
# can find its configuration files.
#

#
# ServerType is either inetd, or standalone. Inetd mode is only supported on
# Unix platforms.
#
ServerType standalone

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE! If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the LockFile documentation
# (available at );
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
ServerRoot "/var/www"

#
# The LockFile directive sets the path to the lockfile used when Apache
# is compiled with either USE_FCNTL_SERIALIZED_ACCEPT or
# USE_FLOCK_SERIALIZED_ACCEPT. This directive should normally be left at
# its default value. The main reason for changing it is if the logs
# directory is NFS mounted, since the lockfile MUST BE STORED ON A LOCAL
# DISK. The PID of the main server process is automatically appended to
# the filename.
#
#LockFile logs/accept.lock
[ ric_man @ mymachine ~/test ] 507 $ grep -v ^# web.txt|grep -v '^[[:space:]]*$'
ServerType standalone
ServerRoot "/var/www"


Just in case you are confused with what I have done above - I have outputted the contents of the file first, and then run the command over it. This is to illustrate the amount of removing that the line does.

No comments:

Post a Comment