Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.

Slides:



Advertisements
Similar presentations
Lecture 5  Regular Expressions;  grep; CSE4251 The Unix Programming Environment.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CSCI 330 T HE UNIX S YSTEM Regular Expressions. R EGULAR E XPRESSION A pattern of special characters used to match strings in a search Typically made.
Regular Expressions grep
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 31: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
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 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Shell Script Examples.
Chapter 4: UNIX File Processing Input and Output.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
Last Updated March 2006 Slide 1 Regular Expressions.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
System Programming Regular Expressions Regular Expressions
Globalisation & Computer systems Week 7 Text processes and globalisation part 1: Sorting strings: collation Searching strings and regular expressions Practical:
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.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
CS 403: Programming Languages Lecture 21 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
REGULAR EXPRESSIONS. Lexical Analysis Lexical analysers can be constructed by programs such as LEX These programs employ as input a description of the.
1 Lecture 5 Additional useful commands COP 3353 Introduction to UNIX.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/15.
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.
Quiz 30 minutes 10 questions No talking, texting, collaboration, etc…
Introduction to Unix – CS 21 Lecture 4. Lecture Overview * cp, mv, and rm Looking into files The file command head and tail cat and more What we’ve seen.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
What is a language? An alphabet is a well defined set of characters. The character ∑ is typically used to represent an alphabet. A string : a finite.
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.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
Appendix A: Regular Expressions It’s All Greek to Me.
Sys Prog & Scrip - Heriot Watt Univ 1 Systems Programming & Scripting Lecture 12: Introduction to Scripting & Regular Expressions.
May 2008CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
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
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
By Corey Stokes 9/14/10. What is grep? Global Regular Expression Print grep is a command line search utility in Unix Try: Search for a word in a.cpp file.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
Finding Things Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 5 – Regular Expressions, grep, Other Utilities.
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
What is grep ?  % man grep  DESCRIPTION  The grep utility searches text files for a pattern and prints all lines that contain that pattern. It uses.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
CSC 352– Unix Programming, Fall 2011 November 8, 2011, Week 11, a useful subset of regular expressions, grep and sed, parts of Chapter 11.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
May 2006CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
ICS611 Lex Set 3. Lex and Yacc Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the.
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
CSC 352– Unix Programming, Spring 2016
Looking for Patterns - Finding them with Regular Expressions
Regular Expression - Intro
Lecture 9 Shell Programming – Command substitution
CSC 352– Unix Programming, Fall 2012
Folks Carelli, Instructor Kutztown University
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
An Overview of Grep and Regular Expression
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CSE 303 Concepts and Tools for Software Development
CSCI The UNIX System Regular Expressions
Regular Expressions grep Familiy of Commands
Lab 8: Regular Expressions
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
Presentation transcript:

Introduction to Unix – CS 21 Lecture 6

Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1

Homework Review Any questions? Couple of points Timestamps are unreliable because touch can make them anything you want The different compression algorithms work better on different types of data

More Thorough Explanation Of Wildcards For The Shell * Match zero or more characters of any type By itself, * will match everything in the current directory ? Matches exactly one character of any type Character sets and range Will match one character in the set [abh] [0-4] [a-kL-M]

Examples

Examples Of ? Usage

Examples Of Character Range Usage

grep – Global / Regular Expressions / Pattern Searches the internals of files and tries to match patterns Used to see if a file contains data you are looking for Will print out every line that contains a match for that pattern Usage: grep [OPTIONS] pattern [FILE]

Common Flags -i Case insensitive (upper and lower cases are treated the same) -n Print out the line numbers -r Recursively traverse the directory -v Invert the results (show all non-matching lines)

Easiest grep Usage The easiest way to use grep is also the most common way to use grep Search files for occurrences of a string (word) The pattern you search for can simply be a word

First grep Examples

Regular Expressions grep can be used to find words that match a certain pattern, not just a given word The language of regular expressions is used to describe these patterns This includes wildcards, repetitions, and complex patterns

How grep Views Regular Expressions Unfortunately, grep’s regular expressions are completely different than the shell wildcards Some of the symbols are the same, but they are used in different ways Always use quotes (‘) so that the wildcards are interpreted by grep and not the shell

Notation ^ Beginning of the line – left rooted $ End of the line – right rooted. Any single character [xy] Any character in the set [^a-z] Any character not in the set B* Zero or more occurrences of B

Examples.* Zero or more of any character Will match any pattern ^ab* Any line that starts with a and has zero or more b’s immediately following a abbbb abb

Working Examples

More Examples [0-9] Any number bye$ The pattern “bye” located at the end of the line Hello and goodbye

Working Examples

One More Slide Of Examples… [^g]$ Match any line that does not end in g [:alpha:]* Any word that contains zero or more alphabetic characters

Wait a minute…

Inverting The Answers grep –v ‘this’ testFile Will find all lines that do not contain the word this Works exactly the same with regular expressions grep –v ‘[^g$]’ testFile Finds all lines that end in g

Working Example

grep Versus egrep In order to match patterns more specifically (instead of zero or more matches as we saw previously), we need egrep egrep stands for Extended grep

Additional egrep Symbols a? Zero or one occurrence of a a+ One or more occurrences of a a|b a or b () Used for nesting

Advanced Examples word? Will match wor, or word ‘ [01]+ ‘ Will match any binary number ‘[0-9]+| [a-f]+ ‘ Will match any number or any word with only a, b, c, d, e, or f

Working Example

More Advanced Examples ‘[a-z]+ [a-z]? [a-z]+’ Will match any two words that may or may not have a single character between them

Working Example

Example With Everything On It ( [0-9][^0-9]+$)|(^bc*) Matches every line that ends with a word that starts with a number or begins with b followed by any number of c’s

Working Example

Tricks To Consider Regular expressions will seem at first to match patterns that you don’t want Think about spaces Think about zero occurrences Think about just one occurrence Making regular expressions do what you want is not easy!

Why Would You Ever Want To Use This? Most of the time, you can get by just using grep and searching for a specific word Searching for all instances of well formatted data requires regular expressions and egrep

In Lab Today File redirection and piping practice Creating regular expressions and pattern matching with grep and egrep Some applications where it will be useful