CIS 218 Advanced UNIX 1
Advanced UNIX CIS 218 Advanced UNIX Regular Expressions See also: - - PowerPoint PPT Presentation
Advanced UNIX CIS 218 Advanced UNIX Regular Expressions See also: - - PowerPoint PPT Presentation
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
CIS 218 Advanced UNIX 2
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
CIS 218 Advanced UNIX 3
vi Commands using Strings
/text
search forward for text
:s/old/new/g
replace every occurrence
- f old by new
:1,.s/fc/function/g
fc replaced by function between
line 1 and current
CIS 218 Advanced UNIX 4
Strings Examples /ring/ ring, spring, ringing /Thurs/ Thursday, Thursday’s /or not/ poor nothing
CIS 218 Advanced UNIX 5
Regular Expressions (REs)
A RE is a string with special characters
that defines one or more strings.
Special characters in vi:
. [...] (with - and ^) * ^ and $ \
CIS 218 Advanced UNIX 6
‘.’ Special Character
matches any single character
RE Examples /.ing/ singing, ping / .alk/ will talk, may balk
CIS 218 Advanced UNIX 7
‘[...]’ 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
CIS 218 Advanced UNIX 8
RE Examples /[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!
CIS 218 Advanced UNIX 9
‘*’ Special Character
Match 0 or more occurrences of a character
RE Examples /ab*c/ ac, abc, abbc, debbcaabbbc /ab.*c/ abc, abxc, ab45c, xab 756.345 x cat /[a-zA-Z ]*/
- 1. any string without nums
- r punctuation!
CIS 218 Advanced UNIX 10
Longest Match Possible
RE Examples /(.*)/ Get (this) and (that); /([^)]*)/ Get (this) and (that); /s.*ing/ singing songs, singing more
CIS 218 Advanced UNIX 11
‘^’ and ‘$’ Special Characters
‘^’ matches a string at the beginning of a
line
‘$’ matches a string at the end of a line
CIS 218 Advanced UNIX 12
RE Examples /^T/ This line..., That Time..., In Time /^+[0-9]/ +5 +45.72, +759 Keep this... /:$/ ...below: ...:+++:
CIS 218 Advanced UNIX 13
‘\’ Special Character
‘\’ can be used to quote a special character
to make it represent itself:
\\ \* \. etc.
CIS 218 Advanced UNIX 14
RE Examples /end\./ The end., send., /\\/ \ /\*/ an asterisk (*) /\[5\]/ it was five [5] /and\/or/ and/or
CIS 218 Advanced UNIX 15
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
CIS 218 Advanced UNIX 16
Full (Extended) Regular Expressions
Default on most current UNIX versions Extended form of RE used by egrep
(and some other commands) The additional special characters:
+
? |
Can use ‘+’, ‘?’, and ‘*’ with parentheses
(...)
CIS 218 Advanced UNIX 17
‘+’ Special Character
Matches 1 or more occurrences of a
character RE Examples ‘ab+c’ yabcw, abbc57 ‘(ab)+c’ zabcd, ababc!
longest match possible rule applies!
CIS 218 Advanced UNIX 18
‘?’ Special Character
Matches 0 or 1 occurrences of a character
RE Examples ‘ab?c’ back, abcdef ‘(ab)?c/ xc, abcc
CIS 218 Advanced UNIX 19
‘|’ Special Character
means ‘or’; used between two REs
RE Examples ‘ab|ac’ ab, ac, abac ‘^Exit|^Quit’ Exit..., Quit..., No Exit ‘(D|N)\. Jones’ P.D. Jones, N. Jones
CIS 218 Advanced UNIX 20
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
CIS 218 Advanced UNIX 21
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