CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.

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

1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
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
1 CSE 303 Lecture 7 Regular expressions, egrep, and sed read Linux Pocket Guide pp , 73-74, 81 slides created by Marty Stepp
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller
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 Lecture 04 Outline Change your shell Sed and awk Tricks with Bash scripts Assignment 1 discussion 1CS 311 Operating SystemsLecture 04.
Scripting Languages CS351 – Programming Paradigms.
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
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.
Advanced File Processing
Using the Unix Shell There is No ‘Undelete’. The Unix Shell “A Unix shell is a command-line interpreter or shell that provides a traditional user interface.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
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.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Computer Programming for Biologists Class 10 Dec 5 th, 2014 Karsten Hokamp
Computer Science 101 Introduction to Programming.
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.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
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.
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.
Sed Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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 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.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
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 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.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
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
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
CSC 352– Unix Programming, Spring 2016
CST8177 sed The Stream Editor.
CSE 374 Programming Concepts & Tools
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall 2012
What is Bash Shell Scripting?
Guide To UNIX Using Linux Third Edition
Unix Talk #2 (sed).
CSE 303 Concepts and Tools for Software Development
CSE 303 Concepts and Tools for Software Development
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup

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 2

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… 3

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 4

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. 5

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] 6

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. 7

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. 8

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 9

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. 10