Upload
brenda-lamb
View
215
Download
1
Embed Size (px)
Citation preview
Review
• Please hand in your practicals and homework• Regular Expressions with grep
Today
• Quizzes Back• Extra Credit• More regular expressions– sed
• Cheatsheets!• And better!
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…
So Compare 2 Whitepapers
• Either of these:• Implementing RHEL 6 on HP Servers • Intel HA RHEL Solution
• To either of these:• RHEL Value Compared to Win2k8• RHEL Migration Best Practices
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
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
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
Character Class Practice
• grep ‘\w’ teams.txt
• What is the command to match any line that starts with a digit?
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
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
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
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
Only Difference is –n
So What Does This Do?
• sed -n ‘/[s]ea/p’ teams.txt
• sed -n ‘/^Bears.*Superbowl$/p’ inMyDreams.txt
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
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 |
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!
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
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
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
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.”