1 / 33

Introduction to Unix – CS 21

Introduction to Unix – CS 21. Lecture 6. Lecture Overview. Homework questions More on wildcards Regular expressions Using grep Quiz #1. Homework Review. Any questions? Couple of points Timestamps are unreliable because touch can make them anything you want

Download Presentation

Introduction to Unix – CS 21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Unix – CS 21 Lecture 6

  2. Lecture Overview • Homework questions • More on wildcards • Regular expressions • Using grep • Quiz #1

  3. Homework Review • Any questions? • Couple of points • Timestamps are unreliable because touch can make them anything you want • The different compression algorithms work better on different types of data

  4. More Thorough Explanation Of Wildcards For The Shell • * • Match zero or more characters of any type • By itself, * will match everything in the current directory • ? • Matches exactly one character of any type • Character sets and range • Will match one character in the set • [abh] [0-4] [a-kL-M]

  5. Examples

  6. Examples Of ? Usage

  7. Examples Of Character Range Usage

  8. grep – Global / Regular Expressions / Pattern • Searches the internals of files and tries to match patterns • Used to see if a file contains data you are looking for • Will print out every line that contains a match for that pattern • Usage: grep [OPTIONS] pattern [FILE]

  9. Common Flags • -i • Case insensitive (upper and lower cases are treated the same) • -n • Print out the line numbers • -r • Recursively traverse the directory • -v • Invert the results (show all non-matching lines)

  10. Easiest grep Usage • The easiest way to use grep is also the most common way to use grep • Search files for occurrences of a string (word) • The pattern you search for can simply be a word

  11. First grep Examples

  12. Regular Expressions • grep can be used to find words that match a certain pattern, not just a given word • The language of regular expressions is used to describe these patterns • This includes wildcards, repetitions, and complex patterns

  13. How grep Views Regular Expressions • Unfortunately, grep’s regular expressions are completely different than the shell wildcards • Some of the symbols are the same, but they are used in different ways • Always use quotes (‘) so that the wildcards are interpreted by grep and not the shell

  14. Notation • ^ • Beginning of the line – left rooted • $ • End of the line – right rooted • . • Any single character • [xy] • Any character in the set • [^a-z] • Any character not in the set • B* • Zero or more occurrences of B

  15. Examples • .* • Zero or more of any character • Will match any pattern • ^ab* • Any line that starts with a and has zero or more b’s immediately following • a • abbbb • abb

  16. Working Examples

  17. More Examples • [0-9] • Any number • 1002 • 0909 • bye$ • The pattern “bye” located at the end of the line • Hello and goodbye

  18. Working Examples

  19. One More Slide Of Examples… • [^g]$ • Match any line that does not end in g • [:alpha:]* • Any word that contains zero or more alphabetic characters

  20. Wait a minute…

  21. Inverting The Answers • grep –v ‘this’ testFile • Will find all lines that do not contain the word this • Works exactly the same with regular expressions • grep –v ‘[^g$]’ testFile • Finds all lines that end in g

  22. Working Example

  23. grep Versus egrep • In order to match patterns more specifically (instead of zero or more matches as we saw previously), we need egrep • egrep stands for Extended grep

  24. Additional egrep Symbols • a? • Zero or one occurrence of a • a+ • One or more occurrences of a • a|b • a or b • () • Used for nesting

  25. Advanced Examples • word? • Will match wor, or word • ‘ [01]+ ‘ • Will match any binary number • ‘[0-9]+| [a-f]+ ‘ • Will match any number or any word with only a, b, c, d, e, or f

  26. Working Example

  27. More Advanced Examples • ‘[a-z]+ [a-z]? [a-z]+’ • Will match any two words that may or may not have a single character between them

  28. Working Example

  29. Example With Everything On It • ( [0-9][^0-9]+$)|(^bc*) • Matches every line that ends with a word that starts with a number or begins with b followed by any number of c’s

  30. Working Example

  31. Tricks To Consider • Regular expressions will seem at first to match patterns that you don’t want • Think about spaces • Think about zero occurrences • Think about just one occurrence • Making regular expressions do what you want is not easy!

  32. Why Would You Ever Want To Use This? • Most of the time, you can get by just using grep and searching for a specific word • Searching for all instances of well formatted data requires regular expressions and egrep

  33. In Lab Today • File redirection and piping practice • Creating regular expressions and pattern matching with grep and egrep • Some applications where it will be useful

More Related