CSC 352– Unix Programming, Fall 2011 November 8, 2011, Week 11, a useful subset of regular expressions, grep and sed, parts of Chapter 11.

Slides:



Advertisements
Similar presentations
CST8177 sed The Stream Editor. The original editor for Unix was called ed, short for editor. By today's standards, ed was very primitive. Soon, sed was.
Advertisements

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
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
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
Linux+ Guide to Linux Certification, Second Edition
Quotes: single vs. double vs. grave accent % set day = date % echo day day % echo $day date % echo '$day' $day % echo "$day" date % echo `$day` Mon Jul.
CS 497C – Introduction to UNIX Lecture 10: The vi/vim Editor Chin-Chih Chang
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
CSC 4630 Meeting 2 January 22, Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.
UNIX Filters.
Filters using Regular Expressions grep: Searching a Pattern.
Shell Script Examples.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
System Programming Regular Expressions Regular Expressions
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
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.
Sed sed is a program used for editing data. It stands for stream editor. Unlike ed, sed cannot be used interactively. However, its commands are similar.
10/18/2015Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 2 (UNIX) 10/18/2015Course material created by D. Woit 1.
Linux+ Guide to Linux Certification, Third Edition
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.
(Stream Editor) By: Ross Mills.  Sed is an acronym for stream editor  Instead of altering the original file, sed is used to scan the input file line.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
January 23, 2007Spring Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip.
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 and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
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.
Review Please hand in your practicals and homework Regular Expressions with grep.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Introduction to sed. Sed : a “S tream ED itor ” What is Sed ?  A “non-interactive” text editor that is called from the unix command line.  Input text.
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(
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 5 – Regular Expressions, grep, Other Utilities.
Linux+ Guide to Linux Certification, Second Edition
FILTERS USING REGULAR EXPRESSIONS – grep and sed.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Copyright 2009 der.hans intro over a decade as sysadmin, programmer, analyst, data janitor currently director of engineering for a startup adjunct instructor.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
CIRC Summer School 2016 Baowei Liu
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
CSE 374 Programming Concepts & Tools
CSC 352– Unix Programming, Spring 2016
Advanced File Processing
CIRC Summer School 2017 Baowei Liu
CST8177 sed The Stream Editor.
CIRC Winter Boot Camp 2017 Baowei Liu
Regular Expression - Intro
BASIC AND EXTENDED REGULAR EXPRESSIONS
Lecture 9 Shell Programming – Command substitution
CSC 352– Unix Programming, Fall 2012
Unix Scripting Dave Yearke
CSC 352– Unix Programming, Spring 2016
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Unix Talk #2 (sed).
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Unix Scripting Session 2 March 13, 2008.
LPI Linux Certification
Presentation transcript:

CSC 352– Unix Programming, Fall 2011 November 8, 2011, Week 11, a useful subset of regular expressions, grep and sed, parts of Chapter 11

Motivation In assignment 4 you will write a shell script to inspect all regular files in and below directory (including subdirectories) for a string pattern. To get a list of files containing a pattern, you can use the find command to find all regular files, and grep to search for the pattern. Your script will then iterate through this list of files and, one at a time, use sed to replace all occurrences of the pattern with a new string.

Finding files with pattern Start out with a manual find command. find JavaLang -type f –print # looks for all regular files find JavaLang -type f –name “*.java” –print # use name Use grep with above in back ticks for file list. grep interface `find JavaLang -type f -print` grep –l interface `find JavaLang -type f -print` grep –l interface `find JavaLang -type f –print 2>/dev/null` Iterate through files in a for loop. all=`find JavaLang -type f -print 2>/dev/null` matches=`grep -l interface $all 2>/dev/null` for file in $matches; do echo ”FOUND FILE $file"; done

File names with spaces These create problems for above approach. It is necessary to use the “-print0” option of find instead of “-print” and to pipe the stdout of find to xargs -0 in order to package space-containing file names up for downstream commands such as grep. We will not use directories and file names that contain spaces in project 4. Avoid spaces in directory and file names when setting up Unix source code and similar repositories.

Using sed for substitution Replace for loop in previous example line with this. for file in $matches do – sed -e "s/interface/thingy/g" $file > junk.tmp.txt – mv junk.tmp.txt $file done Sed can substitute a string (“thingy’ in this example) for a regular expression pattern. The “/g” portion of sed’s substitute command “s/” says, “Do it globally, throughout each line.”

Grep command line (p. 295) Grep searches for a regular expression in stdin or in a list of files given on the command line. A regular expression is a string expression that describes a set of strings. Grep options include the following. -i is case insensitive -v shows only non-matching line (filters out matches). -l (ell) gives only the distinct file names. -n displays line numbers along with lines.

A few important patterns (p. 299) pattern *0 or more occurrences of previous character pattern.any single character pattern [pqr]any single character from set p, q or r pattern [a-zA-Z] any single character from range a-z or A-Z pattern [^pqr] any single character *not* from set p, q or r pattern ^the start of the string being searched for a match pattern $the end of the string being searched for a match pattern \escapes the next character so it is treated as a regular char These are the most useful regular expression patterns available to grep, sed, and for pattern-based searching in emacs and vi. The shell uses so-called “glob-style matching” for strings (*.java), which differ from regular expressions (.*\.java) used by grep, sed, emacs and vi.

Intro to sed Sed is based on early command-line editors in Unix (ed editor), applied to a stream of lines as a filter. All we will cover this semester is using sed to replace occurrences of a pattern in a file with a string. sed –e ‘s/this/that’ # substitutes string “that” for pattern “this”, only once per line, where “this” may contain patterns from the previous slide. Use single quotes in the sed command line if you don’t want the Shell to expand Shell meta-characters such as $. sed –e ‘s/this/that/g’ # global substitute all occurrences of pattern “this” in each line. Sed can read stdin or a file, writes to stdout.