organise
Get to Know Grep
Posted by Gina Trapani at 11:30 PM on July 14, 2008
Now that you've mastered find, programmer Eric Wendelin describes several ways in which grep (Global Regular Expression Print) can make you more productive at the command line.
Tags: command line | grep | organise

Comments (AU Comments · US Comments)
David
Posted July 15, 2008 3:38 PM
I'm just getting into commandlines and scripts etc, can someone point me in the right direction??
I want to 'flag' when a file is being accessed.
I have a program that plays a selected song as an alert.
I want to execute different batchfiles (or something) when different songs are played.
I'm on XP - could Cygwin do this?
rattis
Posted 12:21 AM 15/7/08
@brackenthebox:
I have grep (GNU grep) 2.5.1, which uses -R and -r for recursive on most of my machines. It can do the recursive. I don't think my solaris boxes have the -R or -r though.
So back to the original question to sharfah, is the script you use different from -R?
rattis
brackenthebox
Posted 12:14 AM 15/7/08
@brackenthebox:
Ok, I just looked at the version of grep I always use and realized that it is a modified version from my lab. Sorry about the post.
brackenthebox
brackenthebox
Posted 12:11 AM 15/7/08
@sharfah: Is that different behavior than using the -R flag?
brackenthebox
Duane
Posted 12:09 AM 15/7/08
Learn 'awk' next and you'll have all you need to live on the command line. You'll wonder why people ever take their hands off the keyboard to reach for the mouse!
[duanesbrain.blogspot.com]
Duane
sharfah
Posted 12:04 AM 15/7/08
Here is my rgrep (recursive grep) script which I use all the time to grep within subfolders:
dir=.
pattern="$*"
dirscan()
{
grep $pattern $dir/*
for file in $dir/*
do
if [ -d $file ]; then
dir=$file
dirscan
fi
done;
}
dirscan
sharfah
emwendelin
Posted 1:08 AM 15/7/08
Note: The problem with grep's -r option is that if none of the "pattern-matched" files are found in the current directory, grep stops looking.
IOW, if I say "grep -Hrn function *.js", it won't return anything if there aren't any .js files in the current directory, even if there are .js files in subdirectories.
emwendelin
sharfah
Posted 12:54 AM 15/7/08
@rattis:
Nope, its the same as -R or -r.
I spend most of my time on Solaris 8 and the grep installed doesn't support it.
sharfah
DHT
Posted 1:27 AM 15/7/08
I use a series of bash functions to grep code while excluding other files (like backups, temp files, etc.) My original alias was this:
alias g='find . -type f -a ! -name ".*.sw[op]" -a ! -name "*~" -a ! -name "cscope.*" -print0 | xargs -0 grep --color -In '
Now I have more complex functions that allow me to specify a start directory and/or a filespec, in addition to the grep pattern. This way I'm not limited to searching (almost) all files in just the current directory and its children. If anyone wants them I'll post them, but there are several of them, so it'd be a bit long.
DHT
GuidoFubini
Posted 2:12 AM 15/7/08
find and grep are a truly great combo. For example, to find all the use statements in perl modules under the current directory, I invoke:
find . -name "*.pm" -exec grep -n "use" {} /dev/null \;
This tells you which lines in which files have the word 'use'.
I rolled this into an alias for tcsh, but haven't migrated it to bash:
alias figr 'find . -name \!:1 -exec grep -n \!:2 {} /dev/null \;'
GuidoFubini
DHT
Posted 6:31 AM 15/7/08
OK, here they are. "_g" (note the underscore) is the function that does the actual work. The rest just set various options and eventually call _g(). Also note that _g only has one line of code, regardless of how it looks here.
"g" is the command I normally use, which searches the current directory and its subdirectories for (almost) all files containing the search pattern I give it. Adding a "d" (as in gd, gdf, gdfi) says I want to specify a directory to start from. Adding an "f" says I want to specify the filespec. And adding an "i" says I want a case-insensitive search. Additional options can be given at the command line along with the search text. So if I want to see line numbers, I can add -n like this "gdfi .. \*.xml -n version" to search all *.xml files in the parent directory (and its subdirs) for the case-insensitive text "version" and see the filenames and line numbers of the matches.
I seem to recall it having an issue with directories with spaces in their names, so be watchful of that situation.
It's too bad lifehacker doesn't support code/pre or any of the other preformatted text tags. You'll have to imagine the indentation for yourself.
function _g()
{
find "$GREP_START_DIR" -type f -a -name "$GREP_FILE_PATTERN" -a ! -name ".*.sw[op]" -a ! -name "*~" -a ! -name "cscope.*" -print0 | xargs -0 grep --color -In $GREP_OPTS $@
}
function g()
{
local GREP_START_DIR="."
local GREP_FILE_PATTERN=\*
gdf $GREP_START_DIR "$GREP_FILE_PATTERN" $@
}
function gi()
{
local GREP_OPTS="-i"
g $@
}
function gd()
{
local GREP_START_DIR=$1
shift
local GREP_FILE_PATTERN=\*
gdf $GREP_START_DIR "$GREP_FILE_PATTERN" $@
}
function gdi()
{
local GREP_OPTS="-i"
gd $@
}
function gf()
{
local GREP_START_DIR="."
local GREP_FILE_PATTERN=$1
shift
gdf $GREP_START_DIR "$GREP_FILE_PATTERN" $@
}
function gfi()
{
local GREP_OPTS="-i"
gf $@
}
function gdf()
{
local GREP_START_DIR=$1
shift
local GREP_FILE_PATTERN=$1
shift
_g $@
}
function gdfi()
{
local GREP_OPTS="-i"
gdf $@
}
DHT
emwendelin
Posted 5:16 AM 15/7/08
@DHT: I'd like to see them. If you'd rather not post them then email me at emwendelin < at ] gmail
Thanks!
emwendelin
emwendelin
Posted 8:02 AM 15/7/08
Wow. That's a really cool way to do some quick greps. I think I'll have to tweak to my needs and use that. Thanks!
emwendelin
DHT
Posted 11:09 PM 15/7/08
You're very welcome. I'm glad somebody else can use it!
DHT
pseutro
Posted 3:03 AM 18/7/08
I recently discovered ack.
It's a Perl script that works like grep but supports full Perl regular expressions. It also ignores .svn, .hg, .git, and CVS sub-directories (to name a few) by default.
I find it's a pretty good grep replacement and now has a permanent place in my ~/bin/.
pseutro
ARodX7
Posted 1:05 AM 15/7/08
I've started writing a lot of shell-script lately for a personal server I set up at home.
cat + grep + awk have proven to be such a powerful combo... They are a very nice combination of powerful and flexible tools.
I wish I had these tutorials about 2 moths ago, they would've been so useful!
ARodX7