CHAPTER 15 (Space web hosting) WORKING WITH TEXT FILES then
CHAPTER 15 WORKING WITH TEXT FILES then told it to search for myfile_2draft.doc, and so on. Notice that the example has a backslash before the period separating the file extension from the filename. This indicates to grep that it should interpret the period as an element of the string to be searched for, rather than as a wildcard character, which is how grep usually interprets periods. You don t need to specify a range of characters in this way. You can simply enter whatever selection of characters you want to substitute into the search string. Here s an example: grep ‘myfile[12345].doc’ * This will attempt to find any mention of myfile1.doc, myfile2.doc, myfile3.doc, and so on, in any file within the directory. Here s another example: grep ‘[GgNn]ome’ * This will let you search for the word Gnome within files but takes into account any possible misspelling of the word by people who forget to use the silent G, and any use of uppercase or lowercase. This is only scratching the surface of what regexes can do. For example, many regexes can be combined together into one long search string, which can provide astonishing accuracy when searching. Table 15-3 contains some simple examples that should give you an idea of the power and flexibility of regexes. Table 15-3. Some Examples of Regular Expressions Search String Description ‘document[a-z]’ Returns any lines containing the string document followed by any single letter from the range a through z. ‘document[A-Za-z]’ Returns any lines containing the string document followed by the letters A through Z or a through z. Note that no comma or other character is needed to separate possibilities within square brackets. ‘document.’ Returns any lines containing the string document followed by any other character. The period is used as a wildcard signifying any single character. ‘document[[:digit:]]’ Returns any lines containing the string document followed by any number. ‘document[[:alpha:]]’ Returns any lines containing the string document followed by any character. ‘^document’ Returns any lines that have the string document at the beginning. The caret symbol (^) tells grep to look only at the beginning of each line. ‘document$’ Returns any line that has the string document at the end of the line. The dollar sign symbol ($) tells grep to look for the string only at the end of lines. ‘document[^1-6]’ Returns lines that have the string document in them but not if it s followed by the numbers 1 through 6. When used in square brackets, the caret character (^) produces a nonmatching list a list of results that don t contain the string.