Regular Expressions Todd Kelley CST8207 – Todd Kelley1.

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

Introduction to the bash Shell (Bourne-Again SHell)
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Regular Expressions. u A regular expression is a pattern which matches some regular (predictable) text. u Regular expressions are used in many Unix utilities.
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Script Examples.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
1 Day 16 Sed and Awk. 2 Looking through output We already know what “grep” does. –It looks for something in a file. –Returns any line from the file that.
Introduction to Shell Script Programming
More Scripting Techniques Scripting Process Example Script
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Bash startup files Linux/Unix files stty Todd Kelley CST8207 – Todd Kelley1.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 9 Basic Scripting.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Regular Expression - Intro Patterns that define a set of strings (or, pieces of a string) Not wildcards (similar notion, but different thing) Used by utilities.
Introduction to Regular Expression for sed & awk by Susan Lukose.
Regular Expressions Todd Kelley CST8207 – Todd Kelley1.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
Review of Fundamentals (cont’d) Todd Kelley CST8207 – Todd Kelley1.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
1 Day 18 Bash and the.files. 2 The.files ls shows you the files in your directory –Or at least most of them. –Some files are hidden. Try: ls –a –This.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Copyright 2009 der.hans intro over a decade as sysadmin, programmer, analyst, data janitor currently director of engineering for a startup adjunct instructor.
Regular Expressions Made Easy. Operator List Simple characters match simple characters /a/->a /b/->b /cd/->cd.
CIRC Summer School 2016 Baowei Liu
CSE 374 Programming Concepts & Tools
CSC 352– Unix Programming, Spring 2016
CIRC Summer School 2017 Baowei Liu
CST8177 sed The Stream Editor.
CIRC Winter Boot Camp 2017 Baowei Liu
Regular Expression - Intro
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
Grep Allows you to filter text based upon several different regular expression variants Basic Extended Perl.
CSC 352– Unix Programming, Fall 2012
Guide To UNIX Using Linux Third Edition
CSC 352– Unix Programming, Spring 2016
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Unix Talk #2 (sed).
1.5 Regular Expressions (REs)
Lab 8: Regular Expressions
Introduction to Bash Programming, part 3
Presentation transcript:

Regular Expressions Todd Kelley CST8207 – Todd Kelley1

 Our standard.bashrc and.bash_profile (or.profile )  Our standard script header  Regular Expressions 2

[ -z "${PS1-}" ] && return if [ "${_FIRST_SHELL-}" = "" ] ; then export _FIRST_SHELL=$$ export PATH="$PATH:$HOME/bin" export LC_ALL=en_CA.UTF-8 export LANG=en_CA.UTF-8 # here we put things that # should be done once fi # here we put things that need to be # done for every interactive shell CST8177 – Todd Kelley3

Contains just one line: source./.bashrc CST8177 – Todd Kelley4

#!/bin/sh -u PATH=/bin:/usr/bin ; export PATH # add /sbin and /usr/sbin if needed umask 022 # use 077 for secure scripts CST8177 – Todd Kelley5

 There are two different pattern matching facilities that we use in Unix/Linux: 1. filename globbing patterns match existing pathnames in the current filesystem only 2. regular expressions match substrings in arbitrary input text  We need to pay close attention to which of the two situations we're in, because some of the same special characters have different meanings! CST8177 – Todd Kelley6

 Globbing is used for ◦ globbing patterns in command lines ◦ patterns used with the find command  shell command line (the shell will match the patterns against the file system): ◦ ls *.txt ◦ echo ?????.txt ◦ vi [ab]*.txt  find command (we double quote the pattern so the find command sees the pattern, not the shell): ◦ find ~ -name "*.txt" ◦ in this case, the find command matches the pattern against the file system CST8177 – Todd Kelley7

 IMPORTANT: regular expressions use some of the same special characters as filename matching on the previous slide but they mean different things!  Before we look at regular expressions, let's take a look at some expressions you're already comfortable with: algebraic expressions  Larger algebraic expressions are formed by putting smaller expressions together CST8177 – Todd Kelley8

ExpressionMeaningComment aaa simple expression bbanother simple expression aba x bab is a larger expression formed from two smaller ones concatenating two expressions together means to multiply them b2b2 b x bwe might have represented this with b^2, using ^ as an exponentiation operator ab 2 a x (b x b)why not (a x b) x (a x b)? (ab) 2 (a x b) x (a x b) CST8177 – Todd Kelley9

ExpressionMeaningComment amatch single 'a'a simple expression bmatch single 'b'another simple expression abmatch strings consisting of single 'a' followed by single 'b' "ab" is a larger expression formed from two smaller ones concatenating two regular expressions together means "followed immediately by" and we'll say "followed by" b*b*match zero or more 'b' characters a big difference in meaning from the '*' in globbing! This is the regular expression repetition operator. ab*'a' followed by zero or more 'b' characters why not repeating ('a' followed by 'b'), zero or more times? Hint: think of "ab 2 " in algebra. \(ab\)*('a' followed by 'b'), zero or more times We can use parenthesis, but in Basic Regular Expressions, we use \( and \) CST8177 – Todd Kelley10

ExpressionMatchesEx.Example Matches Comment non-special character itselfx"x"like globbing one expression followed by another first followed by second xy"xy"like globbing.any single character."x" or "y" or "!" or "." or "*" …etc like the '?' in globbing expression followed by * zero or more matches of the expression x*"" or "x" or "xx" or "xxx" …etc NOT like the * in globbing, although.* behaves like * in globbing character classes a SINGLE character from the list [abc]"a" or "b" or "c" like globbing CST8177 – Todd Kelley11

ExpressionMatchesEx.Example Matches Comment ^beginning of a line of text ^x"x" if it’s the first character on the line anchors the match to the beginning of a line $end of a line of text x$"x" if it's the last character on the line anchors the match to the end of a line ^ (but not first) ^a^b"a^b"^ has no special meaning unless its first $ (but not last) $a$b"a$b"$ has no special meaning unless its last CST8177 – Todd Kelley12

ExpressionMatchesEx.Example Matches Comment special character inside [ and ] as if the character is not special [\]"\"conditions: ']' must be first, '^' must not be first, and '-' must be last \ followed by a special character that character with its special meaning removed \."."like globbing \ followed by non- special character the non- special character \a"a"\ before a non- special character is ignored CST8177 – Todd Kelley13

 testing regular expressons with grep on stdin ◦ run grep --color=auto 'expr' ◦ use single quotes to protect your expr from the shell ◦ grep will wait for you to repeatedly enter your test strings (type ^D to finish) ◦ grep will print any string that matches your expr, so each matched string will appear twice (once when you type it, and once when grep prints it) ◦ the part of the string that matched will be colored ◦ unmatched strings will appear only once where you typed them CST8177 – Todd Kelley14

 Regular expressions can be used in awk, grep, vi, sed, more, less, and others  For now, we'll use grep on the command line  We will get into the habit of putting our regex in single quotes on the command line to protect the regex from the shell  Special characters for basic regular expressions: \, [, ],., *, ^, $  can match single quote by using double quotes, as in : grep "I said, \"don't\""  alternatively: grep 'I said, "don'\''t"' CST8177 – Todd Kelley15

 Appendix A in the Sobell Text book is a source of information  You can read under REGULAR EXPRESSIONS in the man page for the grep command - this tells you what you need to know  The grep man page is normally available on Unix systems, so you can use it to refresh your memory, even years from now CST8177 – Todd Kelley16

 examples (try these) ◦ grep ‘ab’ #any string with a followed by b ◦ grep ‘aa*b’ #one or more a followed by b ◦ grep ‘a..*b’ #a, then one or more anything, then b ◦ grep ‘a.*b’ #a then zero or more anything, then b ◦ grep ‘a.b’ # a then exactly one anything, then b ◦ grep ‘^a’ # a must be the first character ◦ grep ‘^a.*b$’ # a must be first, b must be last  Try other examples: have fun! CST8177 – Todd Kelley17