Introduction to Unix – CS 21
-
Upload
zachary-dean -
Category
Documents
-
view
32 -
download
0
description
Transcript of Introduction to Unix – CS 21
![Page 1: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/1.jpg)
Introduction to Unix – CS 21
Lecture 6
![Page 2: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/2.jpg)
Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1
![Page 3: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/3.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/4.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/5.jpg)
Examples
![Page 6: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/6.jpg)
Examples Of ? Usage
![Page 7: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/7.jpg)
Examples Of Character Range Usage
![Page 8: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/8.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/9.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/10.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/11.jpg)
First grep Examples
![Page 12: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/12.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/13.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/14.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/15.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/16.jpg)
Working Examples
![Page 17: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/17.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/18.jpg)
Working Examples
![Page 19: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/19.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/20.jpg)
Wait a minute…
![Page 21: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/21.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/22.jpg)
Working Example
![Page 23: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/23.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/24.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/25.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/26.jpg)
Working Example
![Page 27: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/27.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/28.jpg)
Working Example
![Page 29: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/29.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/30.jpg)
Working Example
![Page 31: Introduction to Unix – CS 21](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/31.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/32.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022020200/56813595550346895d9d0996/html5/thumbnails/33.jpg)
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