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

Slides:



Advertisements
Similar presentations
Lecture 9  sed. sed  sed is a stream-oriented editor the input (file/std input) flows through the program sed and is directed the standard output the.
Advertisements

CSCI 330 T HE UNIX S YSTEM sed - Stream Editor. W HAT IS SED ? A non-interactive stream editor Interprets sed instructions and performs actions Use sed.
การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
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.
LINUX System : Lecture 3 (English-Only Lecture) Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University Acknowledgement.
Regular Expressions grep
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
1 CSE 303 Lecture 7 Regular expressions, egrep, and sed read Linux Pocket Guide pp , 73-74, 81 slides created by Marty Stepp
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller
CS 497C – Introduction to UNIX Lecture 31: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
Filters using Regular Expressions grep: Searching a Pattern.
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.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
System Programming Regular Expressions Regular Expressions
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Title Slide CSS 404/504 The UNIX Operating System (2) By Ralph B. Bisland, Jr.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
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.
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 SED? A non-interactive stream editor Interprets sed instructions and performs actions Use sed to: Automatically perform edits on file(s) ‏ Simplify.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
20-753: Fundamentals of Web Programming 1 Lecture 10: Server-Side Scripting II Fundamentals of Web Programming Lecture 10: Server-Side Scripting II.
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
Unix Programming Environment Part 3-4 Regular Expression and Pattern Matching Prepared by Xu Zhenya( Draft – Xu Zhenya(
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.
BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1.
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 5 – Regular Expressions, grep, Other Utilities.
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.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
FILTERS USING REGULAR EXPRESSIONS – grep and sed.
CSCI 330 UNIX and Network Programming
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
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.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
CIRC Summer School 2016 Baowei Liu
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
CSCI The UNIX System sed - Stream Editor
Department of Computer Science and Engineering
Regular Expression Basic and Extended regular expressions. The grep, egrep. Typical examples involving different regular expressions. By: Abhilash C B.
Looking for Patterns - Finding them with Regular Expressions
CIRC Summer School 2017 Baowei Liu
CIRC Winter Boot Camp 2017 Baowei Liu
The ‘grep’ Command Colin Masterson.
CSC 352– Unix Programming, Spring 2016
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Regular Expressions and Grep
CSCI The UNIX System Regular Expressions
1.5 Regular Expressions (REs)
Presentation transcript:

Lecture 5  Regular Expressions;  grep; CSE4251 The Unix Programming Environment

Regular Expressions (R.E.) What’s R.E. – a set of symbols/rules to describe a string pattern e.g., strings a, aa, aaa, aaaa,.... can be described simply as a+ – facilitate search/replace/delete... Unix commands/utilities that support regular expressions: – grep(fgrep, egrep) - search a file for a string or regular expression – sed - stream editor – awk (nawk) - pattern scanning and processing language

Regular Expression Symbols.any single character *0 or more instances of previous character e.g., a* matches <>, a, aa, aaa, aaaa,... ?0 or 1 instance of previous character + 1 or more instances of previous character ^beginning of a line e.g., ^A matches A only if A is at the beginning of a line $end of a line e.g., A$ matches A only if A is at the end of a line \Turn off the special meaning

[…] matches any of the enclosed characters – [abc] matches a single a b or c – [a-z] matches any of abcdef…xyz [^…] matches any thing not included – [^A-Za-z] matches a single character as long as it is not a letter. Regular Expression Symbols

\<beginning of word e.g., \<abc matches “abcd” but not “dabc” \>end of word e.g., abc\> matches “dabc” but not “abcd” \(…\) stores the pattern inside \( and \) e.g., \(abc\)def matches “abcdef” and stores abc in \1. So \(abc\)def\1 matches “abcdefabc”. Can store up to 9 matches e.g., \(ab\)c\(de\)f\1\2 will match abcdefabde Regular Expression Symbols

X{n} The preceding item X is matched exactly n times. X{n,} The preceding item X is matched n or more times. X{n,m} The preceding item X is matched at least n times, but not more than m times. Repeat

can put a subpattern inside (), and apply +, *, and ? to the entire subpattern. e.g.: a(bc)*d matches “ad”, "abcbcd”, “abcbcbcbcbcbcd”,... Subpattern

More examples x[abc]?xmatches "xax" or "xx“ [abc]* matches "aaaaa" or "acbca" 0*10 matches "010" or " "or "10“ [Dd][Aa][Vv][Ee] – Matches "Dave" or "dave" or "dAVE", – Does not match "ave" or "da"

Review: metacharacters for filename abbreviation (lecture 2) Metacharacters: * ? [ ] ~ * matches anything $ ls *.doc # list all files ending with.doc ? matches single character $ ls ?.doc # list a.doc, b.doc, but not ab.doc […] matches any of the enclosed charactors $ ls [ab].doc # only list a.doc or b.doc $ ls *[cx].doc # list files ending with c.doc or x.doc ~ is a shortcut of your home directory $ cd ~ # change to home directory 9

Difference Interpreted by different programs – filename expansion is done by the shell. – regular expressions are used by commands (programs). Be careful when specifying R.E. on the command line – Good idea to always quote R.E. with special chars (‘’or “”)on the command line – Example: $ grep ‘[a-z]*’ somefile.txt

grep - search for a string grep [-bchilnsvw] PATTERN [filename...] – Read files or standard /redirected input – Search for specified pattern in each line – Send results to the standard output Examples: $ grep ‘^X11’ *- search all files for lines starting with the string “X11” $ grep -v text file - print lines that do not match “text ” Exit status – 0 – pattern found; 1 - not found

grep - options Some useful options -c count number of lines -i ignore case -l list only the files with matching lines -L list files that dose not match -v display lines that do not match -n print line numbers -r recursively search the sub-directories

Regular Expressions for grep \Xturn off any special meaning of character X ^beginning of line $end of line.any single character [...]any of characters in range.… [^....]any single character not in range.… r*zero or more occurrences of r

grep with pipes we can use pipes when a file is expected e.g., $ ls –l | grep a*

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep NW gamefile northwest NW Charles Main

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep '^n' gamefile northwest NW Charles Main northeast NE AM Main Jr north NO Margot Webber

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep '4$' gamefile northwest NW Charles Main

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep TB Savage gamefile grep: Savage: No such file or directory gamefile:eastern EA TB Savage

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep -l 'SE' * gamefile

northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep '5\..' gamefile western WE Sharon Gray southern SO Suan Chin northeast NE AM Main Jr central CT Ann Stephens

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep '\<north' gamefile northwest NW Charles Main northeast NE AM Main Jr north NO Margot Webber

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep '\ ' gamefile north NO Margot Webber

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep -v "Suan Chin" gamefile northwest NW Charles Main western WE Sharon Gray southwest SW Lewis Dalsass southeast SE Patricia Heme eastern EA TB Savage northeast NE AM Main Jr north NO Margot Webber central CT Ann Stephens

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep -c 'west' gamefile 3

gamefile northwestNWCharles Main westernWESharon Gray southwestSWLewis Dalsass southernSOSuan Chin southeastSEPatricia Heme easternEATB Savage northeastNEAM Main Jr northNOMargot Webber centralCTAnn Stephens $ grep -i "$LOGNAME" /etc/passwd zhengm:x:503:504::/home/zhengm:/bin/bash