Lec3Handouts - CS2044 - Advanced Unix Tools Spring 2009...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS2044 - Advanced Unix Tools Spring 2009 Lecture 3 David Slater dms236 at cornell.edu February 23, 2009 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Organization Hopefully you all completed the questionnaire Homework handed out at the end of class due next Friday at 6 pm. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 2
Some Review What does this do? sed ’s/^[ \t]*//g’ myfile Removes all leading whitespace sed ’/^#/d’ /etc/services | more sed ’/^[^#]/d’ /etc/services | more The first displays the file without commends, the second displays only the comments David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Do you grok gawk? gawk gawk is a small pattern matching programing language. The basic structure of an awk program is pattern1 { commands } pattern2 { commands } . . . patterns can be regular expressions! Gawk goes line by line, checking each pattern one by one and if its found, it performs the command. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 4
Simple Examples gawk ’/[Mm]onster/ { print } ’ Frankenstein.txt gawk ’/[Mm]onster/’ Frankenstein.txt gawk ’/[Mm]onster/ {} ’ Frankenstein.txt gawk ’/[Mm]onster/ { print $0 } ’ Frankenstein.txt All print lines of Frankenstein containing the word Monster or monster. If you do not specify an action, gawk will default to printing the line. $0 refers to the whole line. gawk understands extended regular expressions, so we do not need to escape +, ? etc David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Begin and End Gawk allows blocks of code to be executed only once, at the beginning or the end. gawk ’BEGIN { print "Starting search for a monster } /[Mm]onster/ { print; count++ } END { print "Search completed, there are " count " monsters in the book. } ’ Frankenstein.txt gawk does not require variables to be initialized integer variables automatically initialized to 0, strings to ””. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 6
The real power of gawk is its ability to automatically seperate each input line into fields, each referred to by a number. gawk ’ BEGIN { print "Beginning operation"; myval = 0 } /debt/ { myval -= $1 } /asset/ { myval += $1 } END { print myval } ’ infile $0 refers to the whole line $1, $2, . .. $9, $(10) . .. refer to each field The default Field Seperator (FS) is white space. David Slater dms236 at cornell.edu
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/21/2009.

Page1 / 30

Lec3Handouts - CS2044 - Advanced Unix Tools Spring 2009...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online