CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.

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

2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
A Guide to Unix Using Linux Fourth Edition
CS 497C – Introduction to UNIX Lecture 29: - 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.
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.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
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.
UNIX Filters.
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.
Advanced File Processing
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
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.
A Guide to Unix Using Linux Fourth Edition
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
(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, awk, & perl CS 2204 Class meeting 13 *Notes by Mir Farooq Ali and other members of the CS faculty at Virginia Tech. Copyright 2003.
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.
Introduction to Unix – CS 21
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.
Lesson 4-Mastering the Visual Editor. Overview Introducing the visual editor. Working in an existing file with vi. Understanding the visual editor. Navigating.
40 Years and Still Rocking the Terminal!
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
Chapter Twelve sed, awk & perl1 System Programming sed, awk & perl.
16-Dec-15Advanced Programming Spring 2002 sed and awk Henning Schulzrinne Dept. of Computer Science Columbia University.
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
Perl Lab #11 Intro to Perl Debbie Bartlett. Perl Lab #1 2 Perl Practical Extraction and Report Language –or- Pathologically Eclectic Rubbish Lister Created.
Sed. Class Issues vSphere Issues – root only until lab 3.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
Sed and awk CS 2204 Class meeting 13. © Mir Farooq Ali, sed Stream editor Originally derived from ed line editor Used primarily for non interactive.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Administration Working with the BASH Shell.
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.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
Lesson 5-Exploring Utilities
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
Looking for Patterns - Finding them with Regular Expressions
CIRC Summer School 2017 Baowei Liu
CST8177 sed The Stream Editor.
CSE 374 Programming Concepts & Tools
Guide To UNIX Using Linux Third Edition
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
CS 403: Programming Languages
CSE 303 Concepts and Tools for Software Development
Guide To UNIX Using Linux Third Edition
Unix Talk #2 (sed).
Chapter Four UNIX File Processing.
CSE 303 Concepts and Tools for Software Development
CSE 303 Concepts and Tools for Software Development
CSE 303 Concepts and Tools for Software Development
Introduction to Bash Programming, part 3
Presentation transcript:

CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing

10/9/2006CSE 303 Lecture 62 Administravia Assignment 2 is due in one week Office hours –Mine are after class today –I’ll have extra office hours later this week –T/A office hours will be posted by next lecture Follow Turn-in instructions! –username-hw2 –Plain text in readme.txt

10/9/2006CSE 303 Lecture 63 Tip of the Day Replacing Strings in Emacs –M-x query-replace Enter name of string to search for Enter name of string to replace Enter “y” or “n” for each match found –M-x replace-string Same, but does not prompt for each string

10/9/2006CSE 303 Lecture 64 Last Time Powerful Unix Tools –grep –find –diff –wc

10/9/2006CSE 303 Lecture 65 Today String Processing –sed :single lines –More Complex awk : more complex processing perl, python, ruby :general programming Shell Wrap-Up

10/9/2006CSE 303 Lecture 66 Automating File Editing We’ve learned to automate simple tasks –Move around files –Start/Stop processes –Change user environment/permissions But what about… –Changing strings –Repetitive edits to multiple files sed :can help (used in HW2)

10/9/2006CSE 303 Lecture 67 Sed : A Stream EDitor sed –Non-interactive editor –Performs editing actions –Actions defined in a “script” –Stream-oriented Input from file or stdin Script processes each line Output goes to stdout

10/9/2006CSE 303 Lecture 68 How Sed Works Each line copied to “pattern space” All editing commands applied –To data in pattern space –Done in sequence Original input does not change Possible to restrict edits to subset of lines

10/9/2006CSE 303 Lecture 69 Command-Line Syntax Method 1: One-line syntax –sed [options] 'command' file(s) –sed -e 'cmd1' –e 'cmd2' file(s) Method 2: Script file holds commands –sed [options] –f script file(s)

10/9/2006CSE 303 Lecture 610 Search and Replace with Sed Most Common use –sed ‘s/pattern/replacement/g file –Means “replace every (longest) substring that matches pattern with replacement” Common variations –Omit g at end: replace only first match –Put num at end: replace every num th match –sed -n : suppress normal output –Put p at end: print matching lines –sed -r : Use “extended” regular expressions

10/9/2006CSE 303 Lecture 611 Using the Pattern Space Can replace with all or part of a match Special characters in replacement –& : Entire pattern space –\1 : String that match 1 st set of parentheses –\2 : String that match 2 nd set of parentheses –…

10/9/2006CSE 303 Lecture 612 Examples Not so useful –sed 's/a/b/g' ex1.txt –sed 's/a/b/' ex1.txt –sed 's/a/b/2' ex1.txt –sed -n 's/a/b/2p' ex1.txt More useful –sed 's/.*Linux \(.*\).*/\1:/' ex2.txt –sed 's/.*Linux.*/&:/' ex2.txt Newline Note –The \n is not in the text matched against and is (re)- added when printed

10/9/2006CSE 303 Lecture 613 Sed Command Details General syntax of sed commands –[address[,address]][!]command[args] Address specifies range to look at –Address types Line with a particular number e.g.: 3 Lines matching pattern e.g.: /SAVE/ –Using two addresses specifies a range of lines –Using ! Means “use lines not specified in address” Other Commands –d : delete lines

10/9/2006CSE 303 Lecture 614 More Sed Examples Delete lines 3-5: sed '3,5 d' ex3.c Delete lines that don’t contain SAVE –sed '/SAVE/! d' ex3.c Delete lines that start with // –sed '/\/\// d' ex3.c Delete lines between /* and */ –sed '/\/\*/, /\*\// d' ex3.c

10/9/2006CSE 303 Lecture 615 Advanced Sed Features Commands so far: substitute, print, delete Other commands (not used in class) –Append, replace with block, insert, translate –Branch to label –Multi-line patterns –The hold space for fancy editing E.g., copy and paste of lines Need these? Use more powerful language

10/9/2006CSE 303 Lecture 616 Awk Processes text files –File contains records Separated by newline (default) –Records contain fields Separated by spaces (default) Why use awk? –Generate reports from logs –Process results of an experiment (Named after authors, Aho, Weinberger, and Kernighan)

10/9/2006CSE 303 Lecture 617 Running Awk One-line syntax –awk [options] 'script' file(s) Script file –awk [options] –f scriptFile file(s)

10/9/2006CSE 303 Lecture 618 Awk Functionality Script structure –pattern { procedure } Records processed one at a time –Pattern restricts to matching records Fields accessed with $1, …$n BEGIN and END patterns –For procedures before/after processing file

10/9/2006CSE 303 Lecture 619 Advanced Awk Features awk is a very powerful language –Looping constructs –Arrays –Functions –Fancy printing –Powerful math functions Need these? Use Perl, Python, or Ruby

10/9/2006CSE 303 Lecture 620 More Powerful Script Languages Perl, Python, and Ruby Interpreted Write scripts like bash –Prefix script with #! –Make executable with chmod Pre-compiled (fast!)

10/9/2006CSE 303 Lecture 621 Perl Practical Extraction and Report Language –Or “Pathologically Eclectic Rubbish Lister” Language properties –Excellent pattern matching –“Kitchen Sink” syntax –No objects in original version

10/9/2006CSE 303 Lecture 622 Python Fully Object Oriented Simpler Syntax Allows different styles –Procedural –Functional

10/9/2006CSE 303 Lecture 623 Ruby Fully Object Oriented Syntax more similar to Smalltalk Many different ways to do the same things –Harder to debug

10/9/2006CSE 303 Lecture 624 Summary String Processing –sed : quick mods to single lines –awk : more complex record processing –perl, python, ruby : learn one That’s all for the shell! Note: We don’t require you to know how to use any scripting tools other than sed in this class, but we do require you to know when you should consider learning to use one of these tools.

10/9/2006CSE 303 Lecture 625 Next Time Introduction to C!