'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanSpring 2007Lecture 6— sed, command-line-tools wrapupDan Grossman CSE303 Spring 2007, Lecture 6 1'&$%Where are we• learned how to use the shell to run, combine, and write programs• learned regular-expressions (plus more) and grep for findingguided by regexps.– To finish up: reusing previous parts of matched string• Now: sed for find-and-replace guided by regexps• Then: Short plug for awk (not tested or t aught)• Then: Introduction to CDan Grossman CSE303 Spring 2007, Lecture 6 2'&$%Reviewgrep takes a pattern and a file (or stdin)The pattern describes a regexp:• Example: a[bc]*.?.?d*e• Special characters: . ? ^ $ * ( ) [ ] + { } \ | (Someneed escaping; see the man page)grep prints any line that has one or more substrings that match.• Or invert with -v• Or count with -cSo the output is basically a subset of the input; what if we want tochange or add some output. Enter sed...Dan Grossman CSE303 Spring 2007, Lecture 6 3'&$%sedA stream editor ; a little terrible language that proces ses one line at atime. Multi-line manipulations possible but painful.Simple most-common use (and -e optional here):sed -e s/pattern/replacement/g file“For each line of file, replace every (longest) substring that matchespattern with replacement and then print it to standard out.”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-bottomDan Grossman CSE303 Spring 2007, Lecture 6 4'&$%More sedThe replacement text can use \1 ... \9 – very common.Homework hint: To avoid printing the whole line, match the whole lineand 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.Aside: “Line-ending madness” on 3 common operating systems.Dan Grossman CSE303 Spring 2007, Lecture 6 5'&$%Toward full 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 e xactly 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]Dan Grossman CSE303 Spring 2007, Lecture 6 6'&$%The fancy stuffUsually (but not always) when you get to this stuff, your script isunreadable and easier to write in another language.• The “hold” space. One other string that is held across lines. Alsothe “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 assemblylanguage.Your instructor never remem bers the details, but knows roughly whatis possible.Dan Grossman CSE303 Spring 2007, Lecture 6 7'&$%sed su mmaryThe simplest way to do simple find-and-replace using regexps.Programs longer than a few lines are pos sible, but probably the wrongtool.But a line-oriented stream editor is a very common need, and learninghow to use one can help you use a better one.In homework 2, a “one-liner” is plenty.For the rest, see the manual.Dan Grossman CSE303 Spring 2007, Lecture 6 8'&$%awkWe w ill 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 achosen “delimiter”• Easier to process multiple lines at a time (change the end-of-linedelimiter)• Less regexp support; one-liners not as shortDan Grossman CSE303 Spring 2007, Lecture 6 9'&$%string-processing summaryNote many modern scripting languages support grep, sed, and awkfeatures directly in the language, perhaps with better syntax.• Better: combine features• Worse: one big program that “hopefully has everything” insteadof useful small onesWhen all you need to do is simple text manipulation, these tools letyou “hack something up” quicker than, say, Java.But if you need “real” data structures, performance, libraries, etc., youreach their practical limits quickly.Dan Grossman CSE303 Spring 2007, Lecture 6 10'&$%Welcome to CCompared to Java, in rough order of importance• Lower level (less for compiler to do)• Unsafe (wrong programs might do anything)• Not “object-oriente d”• “Standard library” is much smaller.• Many similar control constructs (loops, ifs, ...)• Many syntactic similarities (operators, types, ...)A different world-view and much more to keep track of; Java-likethinking can get you in trouble.Dan Grossman CSE303 Spring 2007, Lecture 6
View Full Document