idiots_guide_to_grep

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: svlug@svlug.org

Date: Fri, 27 Oct 2000 06:13:05 -0600 (MDT)
From: "Karl F. Larsen" <k5di@zianet.com>
Subject: [svlug] Wonderful grep

	Been avoiding grep because it's hard to understand and the man
page is complex too. So played around a few days and found out some simple
things to do and wrote them down.

			Simple Use of Grep
			Karl Larsen
		

Background: 

	Grep is available in Unix and DOS operating systems. It is
software that searches files for key words and displays a user defined
number of lines Before finding the key word and After it. So here is
what I have learned. The manual for grep is quite complex because
grep can do a lot of good things. 

Basic Use:

	The most basic use is simply to search a file for a key word.
This is done using grep on the command line this way:

	$ grep keyword file

Where keyword is a word your looking for in the file named file.
Suppose we have a file that is exactly this named buy:

Jay Bromley
9505 Bryn Mawr Circle
Fort Smith, AR 72908
Sep 22 2000

Dear Jay,
	Please send me both the TiCK1 and Marker Generator kits and
find my check for $22 enclosed. I plan to put the marker in my Ten
Tec Argonaut which needs it. And use the keyer with it too.

73, Karl K5DI

Of course this is so short it is not necessary to use grep but it
will show the method well.

	Suppose we are looking for a message with Marker in it and
just want a single line displayed. You do this with:

	$ grep Marker buy

and it will print to your screen this:

[karl@cannac edit]$ grep Marker buy
        Please send me both the TiCK1 and Marker Generator kits and

Often after doing this you find what you wanted was on the line After
this so to get it you re-do the grep with:

	$ grep -A2 Marker buy

and it prints to your screen as:

[karl@cannac edit]$ grep -A2 Marker buy
        Please send me both the TiCK1 and Marker Generator kits and
find my check for $22 enclosed. I plan to put the marker in my Ten
Tec Argonaut which needs it. And use the keyer with it too.

As you can see it prints the line with the keyword and 2 lines After
that.

	The keyword is case sensitive with Unix grep so Marker is
different than marker.

	A real use of grep is in a search for a message in your
../mail directory. My pine saves mail in files that I have named. So
often I want to find a message and know something about it but not
where it is. Often I only know the e-mail address of the person. That
is enough because it's fairly unique and will bring up the desired
message.

	Last of course you can use "wild cards" to expand the search.
I used $ grep Marker * and it printed out the same line as before
plus told me it didn't look into all the sub directories.

	Grep does a whole lot more of course. But it's hard to
understand unless you have used it in this simple way first. After
using grep for a few weeks I plan to re-read man grep. It *MIGHT*
make more sense then.

done 27 Oct 2000

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu