Username:
Password:
Linux Professional Institute: Level 2 Tutorials

Text & Documentation / Search Text Files Using Regular Expressions pt. 1

Subtitles of the Movie

Exam objective 1.103.7 has a weight of 3, and verifies that candidates are able to manipulate files and text using regular expressions. This includes creating expressions containing several notational elements. It also includes using regular expressions to perform text searches through a file system, or file content. The simplest form of a regular expression is a literal match. You list the characters you want matched, and they must be an exact fit. For example, the expression PAT will match any of the following. It will match any string that contains those three letters in the right order. However, it is case sensitive, and will not match this string. But you can have it match both upper and lower case by using brackets. When you include characters inside brackets the match must be on one of them. This expression will match either an upper case P, or a lower case P followed by the letters AT, so this expression will match any of these. And you can put a number of characters inside the brackets. The matching algorithm picks one letter out of the brackets. This expression will match every one of these, but it will not match more than one letter inside the brackets. It will not match any of these. You can use a tilde as a first character inside the brackets to reverse the action. This bracketed expression will match any one character except P or R. For example, it will match Ant and Act, but it will not match Apt or Art, and you can use a hyphen to create an exclusive range between two characters. For example, this expression will match any lower case alphabetic character, and this expression will match any upper case, or lower case character. And you can use a tilde to reverse any action of matching a range of characters as well. You can follow an item, bracketed or literal, by a repetition operator. A question mark makes the preceding item optional, and if present, it can be matched at most one time. An asterisk means the preceding item is optional, but it may also be matched any number of times. A plus sign means the preceding item must be present once, but it may also be present a number of times. A number inside braces means the preceding item must be matched exactly that number of times. A number in braces followed by a comma means the preceding item must be matched that many or more times. Two numbers in braces means the preceding item must be matched in number of times, but not more than M. Some characters serve a special purpose. For example, a period will match any character. The carat matches the beginning of a line, and the dollar sign matches the end of the line. The Escape character is the backslash. Any character following it is taken as a literal. If, for example, you want to include a literal plus sign in your match, put a backslash in front of the plus sign. The name GRIP stands for Get Regular Expression. You enter it followed by a regular expression pattern, and then a list of file names. In some cases the shell may try to expand your regular expression, so you may want to surround it by quotes to be safe. This command has lots of options. It normally displays the line on which a match is found, but you can use the L option to have it display only the names of the files that contain a match. One option, the Recursive option, is used so often, it has its own command: the RGRIP command will recursively look inside all directories in the file list. And there are other options, also. There are, in fact, so many options with GRIP you will only ever us a subset of what can be done. However, you should review the documentation to find out what it is capable of doing. From the documentation, find out how GRIP matches spaces and tabs and control characters. Construct a small text file and work on getting GRIP to match these things. Use GRIP to search through all the files in the ETC directory to find out where the path is set. Also, which files mention the word 'root'.

Tutorial Information

Course: Linux Professional Institute: Level 2
Author: Arthur Griffith
SKU: 33894
ISBN: 1-934743-79-8
Release Date: 2008-07-21
Duration: 7.5 hrs / 113 lessons
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available