22
Review • Please hand in your practicals and homework • Regular Expressions with grep

Review Please hand in your practicals and homework Regular Expressions with grep

Embed Size (px)

Citation preview

Page 1: Review Please hand in your practicals and homework Regular Expressions with grep

Review

• Please hand in your practicals and homework• Regular Expressions with grep

Page 2: Review Please hand in your practicals and homework Regular Expressions with grep

Today

• Quizzes Back• Extra Credit• More regular expressions– sed

• Cheatsheets!• And better!

Page 3: Review Please hand in your practicals and homework Regular Expressions with grep

Extra Credit

• Whitepaper is now marketing– Interpreting them is fun (sometimes)

• I want (in no more than two pages)– OS and version needed– Dependencies– “Business Case” – what business sectors & what size

companies it’s targeted at, as well as the purpose and benefits

– Limitations or “gotchas”• Get a holistic view of a Linux-based toolset, how it fits

into a company, etc…

Page 5: Review Please hand in your practicals and homework Regular Expressions with grep
Page 6: Review Please hand in your practicals and homework Regular Expressions with grep

Quick Refresh

• The grep commands allows for ‘regular expressions’

• Sets of characters that allow us to define what to match and print– Static characters don’t change– Metacharacters allow for more broad matching– [] matches on one character– Can match on several different things– [A-Zqpr4-8] will match any uppercase letter OR q OR p

OR r OR 4, 5, 6, 7, OR 8

Page 7: Review Please hand in your practicals and homework Regular Expressions with grep

Character Classes

• We can capture “digits” like 1, 42, or 7656784– \d

• We can capture “word characters” like a, the, supercalafragilisticexpalidocious, and asdfjkl– \w

• We can capture “whitespace” like spaces, tabs, and “newlines”– \s

• \d and \w look between whitespaces

Page 8: Review Please hand in your practicals and homework Regular Expressions with grep

Character Type Opposites

• We can capture “non-digits” like whitespace or letter characters– \D

• We can capture “non- word characters” like digits and whitespaces– \W

• We can capture “non-whitespace” like numbers and letters– \S

Page 9: Review Please hand in your practicals and homework Regular Expressions with grep

Character Class Practice

• grep ‘\w’ teams.txt

• What is the command to match any line that starts with a digit?

Page 10: Review Please hand in your practicals and homework Regular Expressions with grep

Implementations

• Each language implements regex’s differently– PERL, Python, Shell, Java, C++, Ruby, etc…

• For example, I can’t explain this:

• \d should be ‘digit’ but I don’t see one in there• \D non-digit returns NOTHING

Page 11: Review Please hand in your practicals and homework Regular Expressions with grep

sed

• So grep deals with lines which makes manipulating data difficult

• There are ‘capture groups’ in grep the () – so ([A-Za-z]+) would catch any letter collection

• But how would you replace?• So we have sed• Stands for ‘stream editor’• Same thing, on any match, perform an action

Page 12: Review Please hand in your practicals and homework Regular Expressions with grep

sed breakdown

• First – let’s match (like grep)• Usage: sed <flag> ‘<1>/<2>/<3>’ <file>• grep ‘[Ss]eattle’ teams.txt• sed -n ‘/[Ss]eattle/p’ teams.txt• -n suppresses full output (the rest of the file)• ‘/[Ss]eattle is our regex• /p’ is print• teams.txt is our file

Page 13: Review Please hand in your practicals and homework Regular Expressions with grep

sed matching

• sed -n ‘/[Ss]eattle/p’ teams.txt• Without –n a copy of the file is printed, and the

matches are duplicated; with –n just matches are printed

• Between ‘/ and /p’ is our regex• [Ss]eattle – same as with grep• The /p’ closes the regex and gives an action –

print; this overrides the –n so our matches go to STDOUT

Page 14: Review Please hand in your practicals and homework Regular Expressions with grep

Only Difference is –n

Page 15: Review Please hand in your practicals and homework Regular Expressions with grep

So What Does This Do?

• sed -n ‘/[s]ea/p’ teams.txt

• sed -n ‘/^Bears.*Superbowl$/p’ inMyDreams.txt

Page 16: Review Please hand in your practicals and homework Regular Expressions with grep

sed replacement

• But sed is mostly used for replacing• Usage: sed <flag> ‘<1>/<2>/<3>/<4>’ <file>• sed ‘s/sea/Sea/g’ teams.txt• No flag• ‘s/ is substitute• sea/ is what we’re matching on• Sea/ will replace what we match on• g’ is ‘global’ – do all; not just one• teams.txt is our file

Page 17: Review Please hand in your practicals and homework Regular Expressions with grep

Your Turn Again

• sed ‘s/Chicago Bears/Best Team Ever/g’ teams.txt

• Our ‘start’ script we made isn’t working. What is the regex to match anything not starting with a S or a K?

• ls /etc/rc3.d |

Page 18: Review Please hand in your practicals and homework Regular Expressions with grep

Extremely Powerful

• The sed utility is frequently said to be a min-programming language in itself

• These are the basic usages we will frequent

• Cheatsheet!

Page 19: Review Please hand in your practicals and homework Regular Expressions with grep

Regex Cheatsheet

• “Anchors” - ^StartAnchor and EndAnchor$• “Sample Patters” – Examples!• “Character Classes” - \d \w \s• “Quantifiers” - * and + and more• “Ranges” – [0-9] [a-z] etc…• “Metacharacters” – bottom left• We’re not covering POSIX Character Classes,

Assertions, String Replacements, or most Special Characters

Page 20: Review Please hand in your practicals and homework Regular Expressions with grep

Finally

• Online Regex Utility• http://boredwookie.net/index.php/blog/onlin

e-bash-regex-checker/ – Will break down a regex and tell you what each

part does– This means I’ll probably ask you to create your

own– But then you can test yours on that utility– And you should use it on the homework/practical

Page 21: Review Please hand in your practicals and homework Regular Expressions with grep

No Class Monday

• It’s Memorial Day– You can come, I won’t be here

• And I will not be responsive this weekend– My brother-in-law is forcing me to go to Packer

territory – I will try to have everything graded on Wednesday,

it may not happen

Page 22: Review Please hand in your practicals and homework Regular Expressions with grep

Own Study• sed • http://www.grymoire.com/Unix/Sed.html • “Sed is the ultimate stream editor. If that sounds strange,

picture a stream flowing through a pipe. Okay, you can't see a stream if it's inside a pipe. That's what I get for attempting a flowing analogy. You want literature, read James Joyce.

• Anyhow, sed is a marvelous utility. Unfortunately, most people never learn its real power. The language is very simple, but the documentation is terrible. The Solaris on-line manual pages for sed are five pages long, and two of those pages describe the 34 different errors you can get. A program that spends as much space documenting the errors as it does documenting the language has a serious learning curve.”