Wednesday, May 27, 2009

Using grep to find lines that don't match your pattern!

This is actually a pretty handy thing to know. I use it to parse config files for shell scripts. The cool thing is it's very easy to do. Let's say you have a config file called my.conf that has two different kinds of lines. Lines that contain things you want and comments. 

It may look like this:

# Users home directory
HDIR='/home/users/jsmith'

# users current directory
CDIR=`pwd`

There may be reasons that I only want to know about the command lines and ignore the comments. To do this I can use grep like this:

user$ grep -v \# my.conf

output:
> HDIR='/home/users/jsmith'
> CDIR=`pwd`


No comments: