Kevin van Zonneveld

On Development and Internet System Engineering

Cat a file, without the comments

| Comments

I recently had to install a couple of squid servers to act as reverse proxies for a webcluster. You can teach the squid server to stand in between in the end users and the webservers, and to store all the static content ( .jpg .flv .css .htm for example ) in the RAM. This saves a lot of I/O and bandwidth on the webservers, and it can really speeds up a site. And the end of the road the webservers’ load dropped with 92%. But before all this worked, I had to run through a massive config file and since the squid config file is their manual at the same time, it’s about 5000 lines long. So I had to find out a way to filter only the important settings from the config file.

This is what i came up with:

1
cat /etc/squid/squid.conf | egrep -v "(^#.*|^$)"

Explained

1
2
3
4
egrep -v      means leave the following out
^#.*          means patterns that begin with a #
|             means or
^$            means patterns that are empty

Updates

update #1

Thanks to an insightfull comment by Darwin Award Winner on this article, here’s a version that would also filter comments with spaces before the #, such as comments that are indented with code blocks:

1
cat /etc/squid/squid.conf | egrep -v "^\s*(#|$)"

Thanks Darwin!

Imported comments

These were imported from my old blog. Please use disqus below for new comments

wrr on 2011-03-31 10:35:02
There are many top quality & lt;a href=& quot;http://www.oakleysaleonline.com& quot;& gt;oakley sunglasses sale& lt;/a& gt; at oakley sunglasses outlet here.Are you a fair or kiosk vendor and want to minimize setup and tear down times? & lt;a href=& quot;http://www.oakleysaleonline.com& quot;& gt;replica oakley sunglasses& lt;/a& gt; offers an inexpensive and creative solution. The & lt;a href=& quot;http://www.buyoakleysunglasses.com& quot;& gt;cheap oakley sunglasses& lt;/a& gt; display holds 16 pairs of wholesale fashion sunglasses and measures L=16in, W=12.75in and H=2in. The great thing about this folding display is the ultra portability; great for & lt;a href=& quot;http://www.buyoakleysunglasses.com& quot;& gt;oakley polarized sunglasses& lt;/a& gt; vendors to quickly and easily transport wholesale sunglasses from one location to another.

moein on 2011-01-09 15:08:52
& lt;div& gt;& lt;h1& gt;& lt;a href=& quot;http://www.nikgraphic.com& quot;& gt;& lt;strong& gt;تبليغات هداياي تبليغاتي طراحي و چاپ& lt;/strong& gt; & amp;#1601;& amp;#1575;& amp;#1585;& amp;#1705;& amp;#1587; & amp;#1575;& amp;#1740;& amp;#1585;& amp;#1575;& amp;#1606;& lt;/a& gt;& lt;/h1& gt;
& lt;a href=& quot;http://www.nikgraphic.com& quot;& gt;& lt;img src=& quot;http://www.nikgraphic.com/admin/uploadgallery/banner21.jpg& quot; alt=& quot;تبليغات هداياي تبليغاتي طراحي و چاپ& quot; width=& quot;32& quot; height=& quot;32& quot; border=& quot;0& quot;& gt;& lt;/a& gt;
& lt;h3& gt;& lt;a href=& quot;http://www.nikgraphic.com& quot;& gt;http://www.nikgraphic.com& lt;/a& gt;& lt;/h3& gt;& lt;/div& gt;

jumbe on 2010-12-22 16:32:42
Thanks for this man.

Kevin on 2009-03-18 20:48:31
@ Bash: Point taken. Though there are two points to be made as well:

- The consistent use of cat can be a good habit to ensure you never modify a file by accident.
- I doubt the extra process will have serious impact on my machine's performance. If it does, it's time for a new machine :)

Bash on 2009-03-17 19:41:36
Useless use of cat award!

http://sial.org/howto/shell/useless-cat/

aptgetupdate.de on 2007-07-31 12:47:43
Thanks.

Kevin on 2007-07-31 00:54:54
@Darwin Award Winner: Those are great suggestions, thanks! I will update the article.

Darwin Award Winner on 2007-07-31 00:35:02
I would suggest \& quot;^\s*(#|$)\& quot; as the pattern, since this would also filter comments with spaces before the #, such as comments that are indented with code blocks.

If you also wanted to remove comments at the ends of lines, you could pipe the output through sed 's/\s+#.*$//'


cat /etc/squid/squid.conf | egrep -v \& quot;^\s*(#|$)\& quot; | sed -e 's/\s+#.*$//'

You could create a script /usr/local/bin/conconf (catconf as in cat config files, or make up a more creative name) with the following contents (remember to make it executable):

#!/bin/sh
cat \& quot;$@\& quot; | egrep -v \& quot;^\s*(#|$)\& quot; | sed -e 's/\s+#.*$//'

Comments