To explore the grep family of pattern-matching utilities.
|
Corning Community College UNIX/Linux Fundamentals |
To explore the grep family of pattern-matching utilities.
The REGULAR EXPRESSIONS section of the grep(1) manual page.
The grep(1) utility has been used in prior assignments in the context of "searching for" a pattern in a file.
grep wasn't just arbitrarily made up out of the blue. It has its roots in the regex functionality of vi/ex. (regex or regexp is an abbreviation of sorts for regular expression). It is actually an acronym:
GREP = Globally [search for] Regular Expression [and] Print
GREP can be used by itself on the command line or in conjunction with other commands (especially with pipes). You always need to supply your regular expression or pattern to grep.
There are a few different 'grep's in existance. Aside from the original grep, there is also egrep and fgrep.
|
|
|
|
|
original grep. Accepts regex in search patterns |
|
|
grep that accepts extended regex metacharacters in search patterns |
|
|
grep that accepts no regex. Just takes literal strings. Also called fast grep. |
So what is the difference? It all depends on what you want to do. For most cases, grep will be suitable.
However, sometimes we wish to add a little more capability to our RegEx patterns. The "e" in egrep stands for "Extended Regular Expressions", and it adds some operators to our list of available Regular Expressions, including:
|
|
|
|
|
group patterns together |
|
|
logical OR, can be used for condensing many patterns together |
|
|
match 1 or more of the preceding |
|
|
match 0 or 1 preceding |
The big advantage of egrep is that we can combine together patterns with an OR, allowing once again for the creation of single patterns that can apply to a wide range of situations. For example:
egrep '(Mon|Tues|Wednes|Thurs|Fri|Sat|Sun)day' somefile.text
Would match any day of the week. By utilizing the parenthesis to group together and the | to OR together, we have a single pattern that can be used to locate items of this pattern.
Copy the pelopwar.txt file from the grep/ subdirectory of the UNIX Public Directory into your home directory.
| | Find all occurances of the substring "coast" |
| | How many matches? |
| | Find all unique occurances of coast as a word (ie not as part of a word) |
| | How many matches? |
| | Find all occurance of the word Dorian that are the last word on the line. |
| | Find all lines that begin with the word Again |
NOTE: Be sure to count the words instead of relying on 'wc -w'. Why would this make a difference?
| | All instances of Athens or Athenians that occur at the beginning of the line. |
| | Accomplish this with one call to egrep using extended regexp's (use your book) |
| | All instances of Corinth or Corinthians that occur at the end of the line. |
| | Accomplish this with just one call to egrep |
| | Try the same regexp that you used in 2a |
| | Did you get any results? |
| | Why did it turn out this way? |
| | Display the first 4 lines of the last command's output that begin with any of your initials. Include your output too. |
| | Using the sort(1) utility, alphabetize the output of the lastlog command and use grep to search for all lines that begin with a lowercase e through g, then print out the first 4 lines of the resulting output. |
All questions in this assignment require an action or response. Please organize your answers into an easily readable format and be prepared to submit the final results to your instructor.
Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy.
When complete, electronically submit your assignment using the "Assignment Submitter", located here:
As always, the class mailing list is available for assistance, but not answers.