CSE 391
Regular Expressions�sed
Slides created by Josh Ervin and Hunter Schafer. �Based off slides made by Marty Stepp, Jessica Miller, Ruth Anderson, Brett Wortzman, and Zorah Fung
ROADMAP
AGENDA
Syntax | Functionality |
[0-9] | Any digit |
[^0-9] | Not any digit |
^ | Beginning of line |
$ | End of line |
classes.txt
CSE142
CSE143
CSE311
CSE391
CSE416
CSE446
CSE507
CHEM142
MATH324
MATH409
MATH461
PHIL322
HIST210
Suppose we have the file classes.txt on the left. Each line contains the name for a single class at UW. What is the full grep command to print all CSE classes which are not at the 300 level?
SED
SED
sed -r ‘s/Taylor/Hunter/’ names.txt
Notes:
names.txt
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Jo, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
Console output
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Hunter
West, Kanye
Jo, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
SED
sed -ri.bak ‘s/Taylor/Hunter/’ names.txt
Notes:
names.txt
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Jo, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
names.txt
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Hunter
West, Kanye
Jo, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
SED
sed -r ‘s/Jo/Hunter/’ names.txt
Notes:
names.txt
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Jo, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
Console output
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Hunter, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
SED
sed -r ‘s/Jo/Hunter/g’ names.txt
Notes:
names.txt
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Jo, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
Console output
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Hunter, Hunter
Lamar, Kendrick
Xiu, Xiu
Mayer, Hunterhn
Legend, Hunterhn
SED
sed -r ‘s/\<Jo\>/Hunter/g’ names.txt
Notes:
names.txt
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Jo, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
Console output
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Hunter, Hunter
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
emails.txt
hschafer@uw.edu
djdan@cs.washington.edu
zorahlf@cs.washington.edu
lruzzo@uw.edu
Suppose we have the file emails.txt on the left. Each line contains the email address of a CS faculty member. What is the full sed command to output to the console all uw.edu addresses replaced with cs.washington.edu addresses?
Console Output
hschafers@cs.washington.edu
djdan@cs.washington.edu
zorahlf@cs.washington.edu
lruzzo@cs.washington.edu
SED
sed -r ‘s/^(.*),(.*)$/\2 \1/g’ names.txt
Notes:
names.txt
Knowles, Zorah
Z, Jay
Grande, Ariana
B, Cardi
Swift, Taylor
West, Kanye
Jo, Jo
Lamar, Kendrick
Xiu, Xiu
Mayer, John
Legend, John
Console output
Zorah Knowles
Jay Z
Ariana Grande
Cardi B
Taylor Swift
Kanye West
Jo Jo
Kendrick Lamar
Xiu Xiu
John Mayer
John Legen
emails.txt
hschafers@uw.edu
djdan@cs.washington.edu
zorahlf@cs.washington.edu
lruzzo@uw.edu
Suppose we have the file emails.txt on the left. Each line contains the email address of a CS faculty member. What is the full sed command to output just the basename of their email address (i.e. before the @ symbol)?
Console Output
hschafers
djdan
zorahlf
lruzzo