Download presentation
Presentation is loading. Please wait.
1
Folks Carelli, Instructor Kutztown University carelli@kutztown.edu
2
Regular Expressions Character sequences used for string matching
Matches longest pattern
3
[] Any single character in []
- Range of characters, ex: [a-z] ^ If first character – any character NOT in [] * Matches zero or more occurrences ^ At beginning of reg exp, matches beginning of line $ At end of reg exp, matches end of line \ Quote a special character (escape character)
4
Matches one or more occurrences
+ Matches one or more occurrences ? Matches zero or one occurrences | Boolean OR
5
\d numerical digits, [0-9]
\w word character, [A-Za-z0-9_] \s whitespace [ \t\n\f\r] \b a word boundary (an anchor, like ^ and $, but for a word, not a line)
6
Alternation (“or”) ex: cat|dog
Repetition { } more granularity than * or + Includes two optional numbers in the braces Minimum Maximum ex: [1-9][0-9]{2,4} matches numbers between 100 and 99999 Group operator ( )
7
POSIX Description [:alnum:] Alphanumeric characters [:alpha:] Alphabetic characters [:ascii:] ASCII characters [:blank:] Space and tab [:digit:] Digits [:lower:] Lowercase letters [:space:] All whitespace characters, including line breaks [:upper:] Uppercase letters [:word:] Word characters (letters, numbers and underscores)
8
grep globally (match a) regular expression (and) print
grep interprets basic regular expressions some special characters have to be escaped ex: ( ) | egrep (grep –E) uses extended regular expressions escaping turns metacharacter meaning off fgrep (grep –F) match literally turn off RE and metacharacters
9
When in the Course of human events, it becomes necessary for
one people to dissolve the political bands which have connected them with another, and to assume, among the Powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation. We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty, and the pursuit of Happiness.
10
Regular Expressions - Try it!
Match any line that contains the word “which” Match any line that contains a comma Match any line that begins with an ‘o’ Match all blank lines Match any line that contains an uppercase letter Match any line that begins with a ‘W’, ‘i’, or ‘t’ Match any line that contains the word “to” only the word, not the embedded characters!
11
Stream EDitor utility sed [-n] program [filelist]
sed [-n] -f program-file [filelist] sed "s/UNIX/Unix/g" thesis > thesis.new
12
some basic options and commands
sed some basic options and commands syntax: sed [option] command [infile] writes to standard out can accept standard input options: -n suppress line printing -e additional command for processing -f read commands from a file commands: s do a substitution g substitute globally p print the current pattern space d delete the current pattern space I ignore case
13
http://www. genuinecoder
14
sed substitution Most common sed usage is for substitution:
sed ‘s/one people/Folks/s’ declaration.txt (all occurrences) sed ‘s/one people/Folks/’ declaration.txt (first occurrence) sed ‘s/one people/Folks/2’ declaration.txt (second occurrence) cat declaration.txt | sed ‘s/one people|one People/Folks/g’ cat declaration.txt | sed ‘s/one People/Folks/ig’ cat declaration.txt | sed ‘s,one People,Folks,ig’ -n (no printing) and –e (multiple commands) sed -n -e 's/one people/Folks/' -e 's/dissolve/melt/p‘ declaration.txt note: ‘p’ causes the matched line to print
15
sed – more… Using sed for pattern matching:
sed –n ‘/pattern/p’ file (works like grep) sed –n ‘1,3p’ file (print first 3 lines) sed ‘1,3d’ file (delete first 3 lines) cat declaration.txt | sed '1,/assume/d‘ (delete from line 1 to line containing “assume”) sed –f sedscript < declaration.txt (make all vowels upper case) sedscript file: s/a/A/g s/e/E/g s/i/I/g s/o/O/g s/u/U/g
16
Try it! List only files in your directory (no directories)
Delete leading blanks at beginning of every line of a file Replace all strings of blanks with one blank Delete empty lines or lines with only spaces in it Delete all lines from the beginning of the file up to, and including the first blank line
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.