Advanced UNIX CIS 218 Advanced UNIX Regular Expressions See also: - - PowerPoint PPT Presentation

advanced unix
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CIS 218 Advanced UNIX 1

Advanced UNIX

CIS 218 Advanced UNIX

Regular Expressions See also: www.regex101.com www.regexr.com

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

CIS 218 Advanced UNIX 4

Strings Examples /ring/ ring, spring, ringing /Thurs/ Thursday, Thursday’s /or not/ poor nothing

slide-5
SLIDE 5

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 $ \

slide-6
SLIDE 6

CIS 218 Advanced UNIX 6

‘.’ Special Character

 matches any single character

RE Examples /.ing/ singing, ping / .alk/ will talk, may balk

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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!

slide-9
SLIDE 9

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!
slide-10
SLIDE 10

CIS 218 Advanced UNIX 10

Longest Match Possible

RE Examples /(.*)/ Get (this) and (that); /([^)]*)/ Get (this) and (that); /s.*ing/ singing songs, singing more

slide-11
SLIDE 11

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

slide-12
SLIDE 12

CIS 218 Advanced UNIX 12

RE Examples /^T/ This line..., That Time..., In Time /^+[0-9]/ +5 +45.72, +759 Keep this... /:$/ ...below: ...:+++:

slide-13
SLIDE 13

CIS 218 Advanced UNIX 13

‘\’ Special Character

 ‘\’ can be used to quote a special character

to make it represent itself:

\\ \* \. etc.

slide-14
SLIDE 14

CIS 218 Advanced UNIX 14

RE Examples /end\./ The end., send., /\\/ \ /\*/ an asterisk (*) /\[5\]/ it was five [5] /and\/or/ and/or

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

(...)

slide-17
SLIDE 17

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!

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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