CSE 374 Programming Concepts & Tools

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

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.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
CS 497C – Introduction to UNIX Lecture 29: - 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.
Scripting Languages CS351 – Programming Paradigms.
Guide To UNIX Using Linux Third Edition
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Filters using Regular Expressions grep: Searching a Pattern.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
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.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
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.
Linux+ Guide to Linux Certification, Third Edition
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
(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.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Chapter 13: sed Say what?. In this chapter … Basics Programs Addresses Instructions Control Spaces Examples.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
Review Please hand in your practicals and homework Regular Expressions with grep.
Introduction to Unix – CS 21
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.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
16-Dec-15Advanced Programming Spring 2002 sed and awk Henning Schulzrinne Dept. of Computer Science Columbia University.
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.
Sed. Class Issues vSphere Issues – root only until lab 3.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 4 – Shell Variables, More Shell Scripts.
Linux+ Guide to Linux Certification, Second Edition
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts.
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.
Programming Languages Meeting 12 November 18/19, 2014.
Linux Administration Working with the BASH Shell.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
Regular Expressions Copyright Doug Maxwell (
Lesson 5-Exploring Utilities
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
CSE 374 Programming Concepts & Tools
CSC 352– Unix Programming, Spring 2016
Looking for Patterns - Finding them with Regular Expressions
CST8177 sed The Stream Editor.
CIRC Winter Boot Camp 2017 Baowei Liu
CSE 374 Programming Concepts & Tools
Getting Started with C.
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
CSE 390a Lecture 7 Regular expressions, egrep, and sed
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall 2012
Intro to PHP & Variables
Henning Schulzrinne Advanced Programming
What is Bash Shell Scripting?
Guide To UNIX Using Linux Third Edition
Unix Talk #2 (sed).
Chapter Four UNIX File Processing.
CSE 390a Lecture 7 Regular expressions, egrep, and sed
CSE 303 Concepts and Tools for Software Development
Regular expressions, egrep, and sed
CSE 303 Concepts and Tools for Software Development
CSE 303 Concepts and Tools for Software Development
CSE 390a Lecture 7 Regular expressions, egrep, and sed
Software Development Techniques
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Winter 2017 Lecture 6 – sed, command-line tools wrapup UW CSE 374 Winter 2017

Where we are Learned how to use the shell to run, combine, and write programs Learned regular-expressions (plus more) and grep for finding guided by regexps Now: sed for find-and-replace guided by regexps Then: Short plug for awk (not tested or taught) Then: Introduction to C UW CSE 374 Winter 2017

Review grep takes a pattern and a file (or stdin) The pattern describes a regexp: Example: a[bc]*.?.?d*e Special characters: . ? ^ $ * ( ) [ ] + { } \ | (Some need escaping; see the man page) grep prints any line that has one or more substrings that match. Or invert with -v Or count with -c So the output is basically a subset of the input. What if we want to change or add some output? Enter sed… UW CSE 374 Winter 2017

sed A stream editor; a terrible little language that processes one line at a time. Multi-line manipulations possible but painful. Simple most-common use (and -e optional here): sed -e s/pattern/replacement/g file “For each line in file, replace every (longest) substring that matches pattern with replacement and then print it to standard out.” (often want to quote ‘s/…/…/g’ to avoid shell substitutions) Simple variations: omit file: read from stdin omit g: replace only first match sed -n and add p where g is: print only lines with 1 match multiple -e s/.../.../...: apply each left-to-right -f file2: read script from file; apply each line top-to-bottom UW CSE 374 Winter 2017

More sed The replacement text can use \1 . . . \9 – very common Hint: To avoid printing the whole line, match the whole line and then have the replacement print only the part you want Newline note: The \n is not in the text matched against and is (re)-added when printed i.e., lines are read into an “edit buffer” and processed there without the (local system’s) newline Aside: “Line-ending madness” on 3 common operating systems UW CSE 374 Winter 2017

Even more sed “sed lines” can have more: different commands (so far, s for substitution) A couple others: p, d, N Other useful ones use the hold space (next slide) different addresses (before the command) number for exactly that line number first~step (GNU only) (lines are first + n*step) $ last line /regexp/ lines containing a match of regexp a label such as :foo before address or command [:label] [address] [command-letter][more-stuff-for-command] UW CSE 374 Winter 2017

Fancy stuff Usually (but not always) when you get to this stuff, your script is unreadable and easier to write in another language • The “hold” space. One other string that is held across lines. Also the “pattern” space (where the “current line” starts). x, G, H Branches to labels (b and t) Enough to code up conditionals and loops like in assembly language Your instructor never remembers the details, but knows roughly what is possible UW CSE 374 Winter 2017

sed summary The simplest way to do simple find-and-replace using regexps Standard on all Linux/Unix systems, even in limited recovery boot modes Programs longer than a few lines are possible, but probably the wrong tool But a line-oriented stream editor is a very common need, and learning how to use one can help you use a better one In homework 3, a “one-liner” is plenty For the rest, see the manual UW CSE 374 Winter 2017

awk We will skip awk, another useful line-oriented editor. Compared to sed: Much saner programming constructs (math, variables, for-loops, . . . ) Easier to print “fields” of lines, where fields are separated by a chosen “delimiter” Easier to process multiple lines at a time (change the end-of-line delimiter) Less regexp support; one-liners not as short UW CSE 374 Winter 2017

String-processing symmary Many modern scripting languages (perl, python, ruby, et al) support grep, sed, and awk features directly in the language, perhaps with better syntax. Better: combine features Worse: one big program that “hopefully has everything” instead of useful small ones When all you need to do is simple text manipulation, these tools let you “hack something up” quicker than, say, Java But if you need “real” data structures, performance, libraries, etc., you reach their practical limits quickly UW CSE 374 Winter 2017