Searching for complex patterns using 'grep'

This is where we start to get a bit fancy. One good feature of BBEdit is that it allows you to perform complex pattern searches using the function 'grep' ('grep' stands for Get REgular exPression, or something like that). This gives you (some of) the power of a Unix geek, without having to be (much of) one.

  1. We're still trying to find all occurrences of 'did' in Adam's speech. Previously we did this using a two step search, by first looking for all occurrences of 'did', and then searching for all occurrences of '*ADA' in the results of the initial search. This time we'll do the whole search in one step. The advantages of this are twofold:
    a. It's faster, simpler, smarter, and if this isn't enough...
    b. It allows you to still take advantage of BBEdit's context displaying capabilities, which wasn't possible with the multi-step method.
  2. So, sufficiently motivated, you first open the Find dialog again (Search>Find).
  3. First be sure that the folder you want to search is selected, and check the "Multi-File Search" box. Also check the "Use Grep" box.
  4. What we want to search for is lines containing the speaker code "*ADA" and the word 'did'. We don't much care about anything else in the line.
    a.This pattern can be expressed using a 'regular expression', which in this case expresses the pattern *ADA-followed-by-anything-else-followed-by-did.
    b.Grep uses the symbol '.' to stand for 'anything', and it uses the symbol '*' to mean 'any number of occurrences of the preceding symbol'.
    c. Therefore, we can use the string '.*' to stand for 'any amount of anything', and put this between '*ADA' and 'did', to show that we don't care about what occurs between them.
    d. One other thing that we have to worry about is that the string '*ADA' will be misinterpreted by grep. As we have already seen, grep takes '*' to mean 'any number of occurrences of the preceding symbol'. Therefore, we put a '\' in front of the asterisk, indicating that we want the asterisk to be taken literally.
    e. Putting these together, we obtain the regular expression \*ADA.*did, and if we now click "Find All", we'll get all and only the lines we're looking for.
  5. Notice that in this set of results, clicking on a matching line in the upper pane of the window does reveal the appropriate context from the original file in the lower window. This is what we wanted.
  6. OK, big deal, you say. But you've only shown me how to do one very simple thing with grep. I want more power, more flexibility, more, more, more. Well, in this case, you're either going to have to wait for me to write up more about how to generate regular expressions, or you're going to have to pull up the grep instructions and try building your own regular expressions. Luckily, BBEdit comes with a set of (basic) instructions, which also contains a brief summary of grep syntax. To view this, open the file "BBEdit Lite Quickstart" in the BBEdit Lite 4.0 folder, and enjoy!