Advanced UNIX CIS 218 Advanced UNIX Regular Expressions See also: www.regex101.com www.regexr.com 1 CIS 218 Advanced UNIX
Why Regular Expressions? To locate text To change text To delineate metacharacters from ordinary characters To suppress evaluation of metacharacters by the shell Different from filename expansion rules 2 CIS 218 Advanced UNIX
vi Commands using Strings /text search forward for text :s/old/new/g replace every occurrence of old by new fc replaced by :1,.s/fc/function/g function between line 1 and current 3 CIS 218 Advanced UNIX
Strings Examples ring, spring , ringing /ring/ Thursday, Thurs day’s /Thurs/ /or not/ poor nothing 4 CIS 218 Advanced UNIX
Regular Expressions (REs) A RE is a string with special characters that defines one or more strings. Special characters in vi : . [...] (with - and ^) * ^ and $ \ 5 CIS 218 Advanced UNIX
‘.’ Special Character matches any single character RE Examples /.ing/ singing, ping will talk, may balk / .alk/ 6 CIS 218 Advanced UNIX
‘[...]’ Special Characters 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 [...]) ‘ \ ’, ‘*’, ‘$’ loose their special character meaning 7 CIS 218 Advanced UNIX
RE Examples bill , Bill , billed /[bB]ill/ talkative, stink , /t[aeiou].k/ teak , tanker number 6 0, number 8:, /number [6-9]/ get number 9 1, 7, @, ., } , Stop ! /[^a-zA-Z]/ 8 CIS 218 Advanced UNIX
‘*’ Special Character Match 0 or more occurrences of a character RE Examples ac , abc , abbc, debbcaabbbc /ab*c/ abc , abxc , ab45c, /ab.*c/ xab 756.345 x cat 1. any string without nums /[a-zA-Z ]*/ or punctuation ! 9 CIS 218 Advanced UNIX
Longest Match Possible RE Examples /(.*)/ Get (this) and (that); /([^)]*)/ Get (this) and (that); /s.*ing/ singing songs, singing more 10 CIS 218 Advanced UNIX
‘^’ and ‘$’ Special Characters ‘^’ matches a string at the beginning of a line ‘$’ matches a string at the end of a line 11 CIS 218 Advanced UNIX
RE Examples /^T/ This line..., That Time..., In Time /^+[0 - 9]/ +5 +45.72, +759 Keep this... ...below : /:$/ ...:+++: 12 CIS 218 Advanced UNIX
‘ \ ’ Special Character ‘ \ ’ can be used to quote a special character to make it represent itself: \\ \* \. etc. 13 CIS 218 Advanced UNIX
RE Examples The end. , send. , /end\./ /\\/ \ an asterisk ( * ) /\*/ it was five [5] /\[5\]/ /and\/or/ and/or 14 CIS 218 Advanced UNIX
Use of REs in grep Put RE in single quotes ‘...’: $ grep ‘st.ing’ file $ grep ‘ooo*’ file $ grep ‘^T’ file $ grep ‘foo[0 - 9]’ file 15 CIS 218 Advanced UNIX
Full (Extended) Regular Expressions Default on most current UNIX versions Extended form of RE used by e grep (and some other commands) The additional special characters: + ? | Can use ‘+’, ‘?’, and ‘*’ with parentheses (...) 16 CIS 218 Advanced UNIX
‘+’ Special Character Matches 1 or more occurrences of a character RE Examples yabcw, abbc 57 ‘ab+c’ zabcd, ababc ! ‘(ab)+c’ longest match possible rule applies! 17 CIS 218 Advanced UNIX
‘?’ Special Character Matches 0 or 1 occurrences of a character RE Examples ‘ab?c’ back, abcdef xc , abcc ‘(ab)?c/ 18 CIS 218 Advanced UNIX
‘|’ Special Character means ‘or’; used between two REs RE Examples ‘ab|ac’ ab , ac , abac ‘^Exit|^Quit’ Exit ..., Quit..., No Exit P.D. Jones , N. Jones ‘(D|N) \ . Jones’ 19 CIS 218 Advanced UNIX
RegEx Examples cats: cat cattle catalog scrawny cat vacation wildcat (each on a separate line) grep ca cats grep cat cats grep cat? cats grep cat. cats grep a cats grep -v tt fruits grep ^c cats grep ‘t$' cats grep '^' cats 20 CIS 218 Advanced UNIX
RegEx Examples Fruits: apple orange pear peach grape banana blueberry plum (each on a separate line) grep pear fruits grep ea fruits grep a fruits grep -v a fruits grep ^p fruits.txt grep 'e$' fruits.txt grep '^' fruits.txt 21 CIS 218 Advanced UNIX
Recommend
More recommend