String Searches


The First Example

For starters, let's find out how smart tkGrep is. If you haven't already done so, start it up and just click on the run button.

You probably noticed several things happened. The first thing you might have noticed is that the lighthouse

moved it's light beam around. This means that tkGrep is running and executing your command. The next thing you probably noticed is that the entry widgets turned yellow, with entries appearing in them like the ones shown in the figure below.

Rather than chastise you for not filling the entries in, tkGrep assigns default values to the Filename, and the Regular Expression entry widgets. The Filename is the example file that tkGrep uses, with the /home/wesb replaced with your home directory, and the Regular Expression is simply the word TEST. If you look in the Log Window, you will notice the last line of the example file was returned with the word TEST highlighted.

What has tkGrep done to produce this output? After assigning the default values, it builds the grep command and executes it. Grep goes through the example file line by line, looking for matches to the word TEST. When it finds one, it prints out the example file line in the Log Window, with the word highlighted in yellow.

Finding Characters

Probably the most common use of grep is searching for a specific string of characters in a file, called string searching. Lets do the simplest string search possible, a single letter. Click on the Regular Expression entry widget. If the cursor is at the end of the word TEST, press the Backspace key 4 times, or press shift+Home to highlight TEST, then press the Delete key to clear the entry widget. Note that on some X-Window systems, Ctrl+h functions as the backspace key. With a cleared entry widget, just type the single character a with no spaces on the left or right, illustrated in the figure below.

This will be a simple search for all the lines in the example file which contain the letter a. It is important that there are no spaces before, or after, the letter a in the entry widget because they hold a special meaning to grep. If there are no spaces, the following lines of output should appear in the Log Window after clicking on the run button.

Only three lines of output are shown here to save on the picture size. The letter a is highlighted for every occurrence within each line, but only one occurrence is sufficient for tkGrep to display the line. Notice that the following line is not included in the output because it does not include the letter a.

sidky:##sidky:671:671:Emil Sidky:/home/bohr3.1/sidky:/bin/tcsh

How do we know it did not include this line? Well you could do a line by line comparison and discover it did not include the line, or you could click on the Invert Matching radio button and rerun tkGrep.

Another thing that tkGrep has done is to reset the colors of the entry widgets to white since there where no errors in the input. Now lets do the same search, only this time with spaces before and after the letter a as shown below.

First remember to click on the Clear button

if you want to remove the previous output from the Log Window. Now click on the Run button and examine the output.

This is quite a different result from the previous search which returned every line containing the letter a. This time the only line that is returned is the one containing the whole letter a. This demonstrates an important rule to remember when using tkGrep that is often forgotten. If you want any occurrence of the string, then do not include spaces around it. Conversely, spaces before, and after the string, preserve the whole string.


    
Index