CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

21
CIS 218 Advanced UNIX Advanced UNIX Advanced UNIX CIS 218 Advanced UNIX Regular Expression s

Transcript of CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

Page 1: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 1

Advanced UNIXAdvanced UNIXCIS 218 Advanced UNIX

Regular Expressions

Page 2: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 2

Why Regular Expressions?Why Regular Expressions?

To locate textTo locate text To change textTo change text To delineate metacharacters from ordinary characters To suppress evaluation of metacharacters by the shell Different from filename expansion rules

Page 3: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 3

vi Commands using Stringsvi Commands using Strings

/text/text search forward for search forward for texttext

:s/old/new/g:s/old/new/g replace every occurrence replace every occurrence of of oldold by by newnew

:1,.s/fc/function/g:1,.s/fc/function/g fcfc replaced by replaced by functionfunction between between line 1 and currentline 1 and current

Page 4: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 4

StringsStrings ExamplesExamples

/ring//ring/ ringring, sp, springring, , ringringinging

/Thurs//Thurs/ ThursThursday, day, ThursThursday’sday’s

/or not//or not/ popoor notor nothinghing

Page 5: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 5

Regular Expressions (REs)Regular Expressions (REs)

A RE is a string with special characters that dA RE is a string with special characters that defines one or more strings.efines one or more strings.

Special characters in Special characters in vivi::..

[...][...] (with - and ^)(with - and ^)

**

^ and $^ and $

\\

Page 6: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 6

‘‘.’ Special Character.’ Special Character

matches any single charactermatches any single character

RERE ExamplesExamples

/.ing//.ing/ singsinging, ing, pingping

/ .alk// .alk/ willwill talktalk, may, may balk balk

Page 7: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 7

‘‘[...]’ Special Characters[...]’ Special Characters

Match any single character given inside the Match any single character given inside the bracketsbrackets: i.e. [aeiou] is any single vowel: i.e. [aeiou] is any single vowel– ‘‘-’ to specify a range-’ to specify a range– ‘‘^’ to make the range negative^’ to make the range negative

(this meaning for ‘^’ only applies inside [...])(this meaning for ‘^’ only applies inside [...])

‘‘\’, ‘*’, ‘$’ loose their special character mea\’, ‘*’, ‘$’ loose their special character meaningning

Page 8: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 8

RERE ExamplesExamples

/[bB]ill//[bB]ill/ billbill, , BillBill, , billbilleded

/t[aeiou].k//t[aeiou].k/ talktalkative, sative, stinktink,,teakteak, , tanktankerer

/number [6-9]//number [6-9]/ number 6number 60, 0, number 8number 8:, :, get get number 9number 9

/[^a-zA-Z]//[^a-zA-Z]/ 11,, 77, , @@,, .., , }}, Stop, Stop!!

Page 9: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 9

‘‘*’ Special Character*’ Special Character

Match 0 or more occurrences of a characterMatch 0 or more occurrences of a character

RERE ExamplesExamples

/ab*c//ab*c/ acac, , abcabc, , abbcabbc, debbca, debbcaabbbcabbbc

/ab.*c//ab.*c/ abcabc, , abxcabxc, , ab45cab45c,,xxab 756.345 x cab 756.345 x catat

/[a-zA-Z ]*//[a-zA-Z ]*/ 1. 1. any string without numsany string without nums or punctuationor punctuation!!

Page 10: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 10

Longest Match PossibleLongest Match Possible

RERE ExamplesExamples

/(.*)//(.*)/ Get Get (this) and (that)(this) and (that);;

/([^)]*)//([^)]*)/ Get Get (this)(this) and (that); and (that);

/s.*ing//s.*ing/singing songs, singingsinging songs, singing more more

Page 11: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 11

‘‘^’ and ‘$’ Special Characters^’ and ‘$’ Special Characters

‘‘^’ matches a string at the beginning of a lin^’ matches a string at the beginning of a linee

‘‘$’ matches a string at the end of a line$’ matches a string at the end of a line

Page 12: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 12

RERE ExamplesExamples

/^T//^T/ TThis line...,his line...,TThat Time...,hat Time...,In TimeIn Time

/^+[0-9]//^+[0-9]/ +5+5 +45.72, +45.72,+7+759 Keep this...59 Keep this...

/:$//:$/ ...below...below::...:+++...:+++::

Page 13: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 13

‘‘\’ Special Character\’ Special Character

‘‘\’ can be used to quote a special character to ma\’ can be used to quote a special character to make it represent itself:ke it represent itself:\\\\

\*\*

\.\.

etc.etc.

Page 14: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 14

RERE ExamplesExamples

/end\.//end\./ The The end.end., , send.send., ,

/\\//\\/ \\

/\*//\*/ an asterisk (an asterisk (**))

/\[5\]//\[5\]/ it was five it was five [5][5]

/and\/or//and\/or/ and/orand/or

Page 15: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 15

Use of REs in grepUse of REs in grep

Put RE in single quotes ‘...’:Put RE in single quotes ‘...’:

$ grep ‘st.ing’ file$ grep ‘st.ing’ file

$ grep ‘ooo*’ file$ grep ‘ooo*’ file

$ grep ‘^T’ file$ grep ‘^T’ file

$ grep ‘foo[0-9]’ file$ grep ‘foo[0-9]’ file

Page 16: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 16

Full Full (Extended) (Extended) Regular ExpressionsRegular Expressions

Default on most current UNIX versionsDefault on most current UNIX versions Extended form of RE used by Extended form of RE used by eegrepgrep

(and some other commands)(and some other commands)The additional special characters:The additional special characters:

++ ?? || Can use ‘+’, ‘?’, and ‘*’ with parentheses (..Can use ‘+’, ‘?’, and ‘*’ with parentheses (..

.).)

Page 17: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 17

‘‘+’ Special Character+’ Special Character

Matches 1 or more occurrences of a characterMatches 1 or more occurrences of a character

RERE ExamplesExamples

‘ab+c’‘ab+c’ yyabcabcw, w, abbcabbc5757

‘(ab)+c’‘(ab)+c’ zzabcabcd, d, ababcababc!!

longest match possible rule applies!longest match possible rule applies!

Page 18: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 18

‘‘?’ Special Character?’ Special Character

Matches 0 or 1 occurrences of a characterMatches 0 or 1 occurrences of a character

RERE ExamplesExamples

‘ab?c’‘ab?c’ bbacack, k, abcabcdefdef

‘(ab)?c/‘(ab)?c/ xxcc, , abcabccc

Page 19: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 19

‘‘|’ Special Character|’ Special Character

means ‘or’; used between two REsmeans ‘or’; used between two REs

RERE ExamplesExamples

‘ab|ac’‘ab|ac’ abab, , acac, , ababacac

‘^Exit|^Quit’‘^Exit|^Quit’ ExitExit...,...,QuitQuit...,...,No ExitNo Exit

‘(D|N)\. Jones’‘(D|N)\. Jones’ P.P.D. JonesD. Jones, , N. JonesN. Jones

Page 20: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 20

RegEx ExamplesRegEx Examples

cats: cat cattle catalog scrawny cat vacation wildcat (each on a separate line)(each on a separate line)

grep ca catsgrep ca cats grep cat catsgrep cat cats grep cat? catsgrep cat? cats grep cat. cats grep cat. cats grep a cats grep a cats grep -v tt fruits grep -v tt fruits grep ^c cats grep ^c cats grep ‘t$' cats grep ‘t$' cats grep '^' cats grep '^' cats

Page 21: CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.

CIS 218 Advanced UNIX 21

RegEx ExamplesRegEx Examples

Fruits: apple orange pear peach grape banana blueberry plum Fruits: apple orange pear peach grape banana blueberry plum

(each on a separate line)(each on a separate line)

grep pear fruitsgrep pear fruits grep ea fruits grep ea fruits grep a fruits grep a fruits grep -v a fruits grep -v a fruits grep ^p fruits.txt grep ^p fruits.txt grep 'e$' fruits.txt grep 'e$' fruits.txt grep '^' fruits.txt grep '^' fruits.txt