Revision of Grepping About from Wed, 06/05/2009 - 22:46

The revisions let you track differences between multiple versions of a post.

==What On Earth is Grep== As a tool '''"grep"''' is just one of those commands that once you begin to use you just can't do without, nothing in it name suggests what this command does. [http://en.wikipedia.org/wiki/Grep Wikipedia] documents grep as a command line text search utility originally written for Unix. The name is taken from the first letters in global / regular expression / print, a series of instructions for the ed text editor. The '''"grep"''' command searches a file, files or standard input globally through the lines for matching a given regular expression, and prints them to the program's standard output. So in english using the given format of command searchname filename.txt We can using the above formula search any regular text file for any letter, number or word (now why didn't they say that in the first place). The above is the syntax used is not the actual command used itself to search a file for a given expression being searched for. The actual command used would be something like the following.

#> grep choice menuui.txt

The command syntax followed is command = grep searchname = choice filename.txt = menuui.txt together when this command is run from a terminal or console window, provided you have downloaded the file attachment on this page, this is only an example document the file itself can be any file consisting of text.
#> grep choice menuui.txt
echo -n "Enter your menu choice [1-5]: "
*) echo "Opps!!! Please select choice 1,2,3,4, or 5";
Two lines here are returned meaning that two instances of the word '''"choice"''' have been found, obviously the word being searched for can be anything you may want and additionally the name of the file being searched can also be changed provided the file is a text or script file and the command is run in the same directory that the file is located. Now lets use the same command but use '''"-i"''' to ignore the case of the characters being searched unfortunately the word '''"choice"''' does not have any uppercase characters within the text supplied so we need to change the word to look for within this script. So we will change searchname to "menu and use the -i option" as this text word appears in both uppercase and lowercase.

#> grep -i menu menuui.txt

This command runs exactly the same as the first the only difference is the choice of word being searched and we have used the '''"-i"''' option to ignore case and flag all instances of word being searched for.
#> grep -i menu menuui.txt
# Script to create simple menus and take action according to that selected
# menu item
echo " Main Menu "
echo -n "Enter your menu choice [1-5]: "
As you can see from the above example four lines have been returned and the case letter '''"M"''' in the word '''"menu"''' has been ignored, just what we wanted it has also found the word part of "menu" in "menus" if this is what you where looking for then grep has done it's job.

#> grep -w menu menuui.txt

The option -w when used in the above command limits the search to an exact match of the word being searched for. Expanding the search to look at more than one file or directory of files we need to incorporate the path of these files into the syntax and use a wildcard '''"*"''' command searchname pathname/*.txt

#> grep -i menu directory/*.txt