Download presentation
Presentation is loading. Please wait.
Published byMabel Shaw Modified over 9 years ago
1
CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions
2
CIS 218 Advanced UNIX2 Why Regular Expressions? v To locate text v To change text v v To delineate metacharacters from ordinary characters v v To suppress evaluation of metacharacters by the shell v v Different from filename expansion rules
3
CIS 218 Advanced UNIX3 vi Commands using Strings /text search forward for text :s/old/new/g replace every occurrence of old by new :1,.s/fc/function/g fc replaced by function between line 1 and current
4
CIS 218 Advanced UNIX4 StringsExamples /ring/ring, spring, ringing /Thurs/Thursday, Thursday’s /or not/poor nothing
5
CIS 218 Advanced UNIX5 Regular Expressions (REs) v A RE is a string with special characters that defines one or more strings. Special characters in vi :. [...](with - and ^) * ^ and $ \
6
CIS 218 Advanced UNIX6 ‘.’ Special Character v matches any single character REExamples /.ing/singing, ping /.alk/will talk, may balk
7
CIS 218 Advanced UNIX7 ‘[...]’ Special Characters v Match any single character given inside the brackets: i.e. [aeiou] is any single vowel –‘-’ to specify a range –‘^’ to make the range negative (this meaning for ‘^’ only applies inside [...]) v ‘\’, ‘*’, ‘$’ loose their special character meaning
8
CIS 218 Advanced UNIX8 REExamples /[bB]ill/bill, Bill, billed /t[aeiou].k/talkative, stink, teak, tanker /number [6-9]/number 60, number 8:, get number 9 /[^a-zA-Z]/1, 7, @,., }, Stop!
9
CIS 218 Advanced UNIX9 ‘*’ Special Character v Match 0 or more occurrences of a character REExamples /ab*c/ac, abc, abbc, debbcaabbbc /ab.*c/abc, abxc, ab45c, xab 756.345 x cat /[a-zA-Z ]*/1. any string without nums or punctuation!
10
CIS 218 Advanced UNIX10 Longest Match Possible REExamples /(.*)/Get (this) and (that); /([^)]*)/Get (this) and (that); /s.*ing/singing songs, singing more
11
CIS 218 Advanced UNIX11 ‘^’ and ‘$’ Special Characters v ‘^’ matches a string at the beginning of a line v ‘$’ matches a string at the end of a line
12
CIS 218 Advanced UNIX12 REExamples /^T/This line..., That Time..., In Time /^+[0-9]/+5 +45.72, +759 Keep this... /:$/...below:...:+++:
13
CIS 218 Advanced UNIX13 ‘\’ Special Character v ‘\’ can be used to quote a special character to make it represent itself: \\\*\.etc.
14
CIS 218 Advanced UNIX14 REExamples /end\./The end., send., /\\/\ /\*/an asterisk (*) /\[5\]/it was five [5] /and\/or/and/or
15
CIS 218 Advanced UNIX15 Use of REs in grep v Put RE in single quotes ‘...’: $ grep ‘st.ing’ file $ grep ‘ooo*’ file $ grep ‘^T’ file $ grep ‘foo[0-9]’ file
16
CIS 218 Advanced UNIX16 Full (Extended) Regular Expressions v Default on most current UNIX versions Extended form of RE used by egrep (and some other commands) The additional special characters: v +?| v Can use ‘+’, ‘?’, and ‘*’ with parentheses (...)
17
CIS 218 Advanced UNIX17 ‘+’ Special Character v Matches 1 or more occurrences of a character REExamples ‘ab+c’yabcw, abbc57 ‘(ab)+c’zabcd, ababc! v longest match possible rule applies!
18
CIS 218 Advanced UNIX18 ‘?’ Special Character v Matches 0 or 1 occurrences of a character REExamples ‘ab?c’back, abcdef ‘(ab)?c/xc, abcc
19
CIS 218 Advanced UNIX19 ‘|’ Special Character v means ‘or’; used between two REs REExamples ‘ab|ac’ab, ac, abac ‘^Exit|^Quit’Exit..., Quit..., No Exit ‘(D|N)\. Jones’P.D. Jones, N. Jones
20
CIS 218 Advanced UNIX20 RegEx Examples v v cats: cat cattle catalog scrawny cat vacation wildcat v (each on a separate line) v grep ca cats v grep cat cats v grep cat? cats v grep cat. cats v grep a cats v grep -v tt fruits v grep ^c cats v grep ‘t$' cats v grep '^' cats v v
21
CIS 218 Advanced UNIX21 RegEx Examples v Fruits: apple orange pear peach grape banana blueberry plum (each on a separate line) v grep pear fruits v grep ea fruits v grep a fruits v grep -v a fruits v grep ^p fruits.txt v grep 'e$' fruits.txt v grep '^' fruits.txt
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.