Top Banner
Introduction to Unix – CS 21 Lecture 6
33

Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Jan 02, 2016

Download

Documents

Reginald Flynn
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Introduction to Unix – CS 21

Lecture 6

Page 2: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 3: 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

The different compression algorithms work better on different types of data

Page 4: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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]

Page 5: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Examples

Page 6: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Examples Of ? Usage

Page 7: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Examples Of Character Range Usage

Page 8: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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]

Page 9: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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)

Page 10: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 11: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

First grep Examples

Page 12: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 13: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 14: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 15: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 16: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Working Examples

Page 17: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

More Examples [0-9]

Any number 1002 0909

bye$ The pattern “bye” located at the end

of the line Hello and goodbye

Page 18: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Working Examples

Page 19: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 20: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Wait a minute…

Page 21: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 22: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Working Example

Page 23: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 24: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 25: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 26: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Working Example

Page 27: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 28: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Working Example

Page 29: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 30: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Working Example

Page 31: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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!

Page 32: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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

Page 33: Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

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