» Cat a file, without the 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:

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

Explained

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:

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

Thanks Darwin!

 

You probably shouldn't follow me


Like this Article?

I'd appreciate it if you leave a comment, spread the word, or consider a small donation


tags: linux, cat, comments, squid, config file
category: Howto - Webserver
read: 19,745 times

Add comment

(required, shown)(required, not shown)for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

 Track replies: rss feed comments feed

Comments

#8. wrr on 31 March 2011

Gravatar.com: wrrThere are many top quality <a href="http://www.oakleysaleonline.com">oakley sunglasses sale</a> at oakley sunglasses outlet here.Are you a fair or kiosk vendor and want to minimize setup and tear down times? <a href="http://www.oakleysaleonline.com">replica oakley sunglasses</a> offers an inexpensive and creative solution. The <a href="http://www.buyoakleysunglasses.com">cheap oakley sunglasses</a> 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 <a href="http://www.buyoakleysunglasses.com">oakley polarized sunglasses</a> vendors to quickly and easily transport wholesale sunglasses from one location to another.

#7. moein on 09 January 2011

Gravatar.com: moein<div><h1><a href="http://www.nikgraphic.com"><strong>تبليغات هداياي تبليغاتي طراحي و چاپ</strong> &#1601;&#1575;&#1585;&#1705;&#1587; &#1575;&#1740;&#1585;&#1575;&#1606;</a></h1>
<a href="http://www.nikgraphic.com"><img src="http://www.nikgraphic.com/admin/uploadgallery/banner21.jpg" alt="تبليغات هداياي تبليغاتي طراحي و چاپ" width="32" height="32" border="0"></a>
<h3><a href="http://www.nikgraphic.com">http://www.nikgraphic.com</a></h3></div>

#6. jumbe on 22 December 2010

Gravatar.com: jumbeThanks for this man.

#5. Kevin on 18 March 2009

Twitter.com: kvz@ 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 :)

#4. Bash on 17 March 2009

Gravatar.com: BashUseless use of cat award!

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

#3. aptgetupdate.de on 31 July 2007

Default avatar:aptgetupdate.deThanks.

#2. Kevin on 31 July 2007

Default avatar:Kevin@Darwin Award Winner: Those are great suggestions, thanks! I will update the article.

#1. Darwin Award Winner on 31 July 2007

Default avatar:Darwin Award WinnerI would suggest "^\s*(#|$)" 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+#.*$//'


... [more] cat /etc/squid/squid.conf | egrep -v "^\s*(#|$)" | 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 "$@" | egrep -v "^\s*(#|$)" | sed -e 's/\s+#.*$//'