Unformatted text preview:

print NR 0 Print a line number before every line NF 3 print Print every line with three fields print 1 3 Print the first and third fields of each line print Run on 1 5 10 3 7 11 gives 6 data points 6 16667 average 3 92003 standard deviation bin awk f BEGIN n 0 s 0 ss 0 NF 1 n s 1 ss 1 1 END print n data points m s 0 0 n print m average sd sqrt ss n m m n 1 0 print sd standard deviation Print every line action z Statistics in Awk 0 print 1 2 3 pattern z 3 z Kathy 140 3rd field Awk One Liners Kathy 140 Mark 200 Susie 148 5 produces 3 0 print 1 2 3 Run on the awk program Beth 10 0 0 Dan 9 75 0 Kathy 14 0 10 Mark 10 0 20 Susie 8 25 18 Beth 10 0 Kathy 14 0 Input file Each line a record Space separated fields employee pay rate hours worked 0 10 Simple Awk Program Simple Awk Program Bourne Shell Tcl Python Perl Awk The more popular ones COMS W4115 Prof Stephen A Edwards Fall 2006 Columbia University Department of Computer Science Peter Weinberger Often not suitable for large systems Boolean operators True before and after the file Condition String pattern match 103 the 58 of 51 is 49 and 49 a 35 expression 32 The 29 Run on the Tiger reference manual produces gsub remove punctuation for i 1 i NF i count i END for w in count print count w w sort rn Associative Arrays Word Counter Patterns BEGIN END expression regular expression pattern pattern pattern pattern pattern awk scans an input file one line at a time and in order runs each action whose pattern matches pattern action pattern action Awk Program Structure Good for data file manipulation I use it for computing grades and other simple tasks Brian Kernighan Alfred Aho More compact for small programs Named for its three developers Higher level languages Scripting Languages Awk Scripting Languages usr bin perl while chop s g words split foreach words count open SORTER sort nr foreach keys count print SORTER count n Larry Wall s Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister Regular expression support but not as integrated as Perl s Very few things can t be done in Perl f os popen sort nr w for word in count keys f write d s n count word word count for line in fileinput input line re sub r line for word in string split line if not count has key word count word 1 else count word count word 1 1 2i 3 5i Prints x Complex 1 2 y Complex 2 3 x p x add y x p def p self print g gi self r self i def add self a self r self r a r self i self i a i class Complex def init self realpart imagpart self r realpart self i imagpart usr bin env python import fileinput re string os Python Classes Very very extensive collection of libraries Database access CGI HTML for the web Math IPC Time Ported everywhere Ability to make virtually every Unix system call Binary data manipulation Wordcount in Python while while if if unless Many Perl statements come in prefix and postfix form Large collection of libraries but not as big as Perl s Almost has a good type system Fast flexible interpreter Very clean syntax and semantics Despite its flaws it s very powerful while STDIN print print while STDIN print while while defined STDIN print for STDIN print print while defined STDIN Operating system access files directories etc String manipulation Curses Databases Networking CGI HTTP URL mail Mime HTML Tk Cryptography System specific Windows Mac SGI POSIX Good collection of libraries High level lists strings associative arrays iterators Persistent datastructures pickling Packages with separate namespaces Exceptions Classes Good support for programming in the large Python s Merits Perl designed by a sane man Perhaps the most popular scripting language Perhaps too many Equivalent ways to print STDIN Python So Why Perl usr bin perl while line chop line line s g words split s line foreach word words count word open SORTER sort nr foreach word keys count print SORTER count word word n Understandable wordcount in Perl There s more than one way to do it Excellent regular expression support More complicated data structures possible even classes Larger more flexible language than Awk Good for text processing and other tasks Strange semantics Henious syntax Wordcount in Perl Perl Perl better known An Editable Graph Set up bottom control buttons frame w buttons pack w buttons side bottom fill x pady 2m button w buttons dismiss text Dismiss command destroy w button w buttons code text See Code command showCode w pack w buttons dismiss w buttons code side left expand 1 set count Stephen 1 Procedures proc sum3 a b c return expr a b c An Editable Graph Set up the main window set w plot catch destroy w toplevel w wm title w Plot Demonstration wm iconname w Plot positionWindow w set c w c while gets stdin line 0 regsub all line line foreach word line if info exists count word set count word 1 else incr count word set f open sort rn w foreach word array names count puts f count word word An Editable Graph Text description at top label w msg font font wraplength 4i justify left text This window displays a canvas widget containing a simple 2 dimensional plot You can doctor the data by dragging any of the points with mouse button 1 pack w msg side top lappend foo 1 lappend foo 2 foreach i foo puts i print 1 then 2 Draw axes set plotFont Helvetica 18 c create line 100 250 400 250 width 2 c create line 100 250 100 50 width 2 c create text 225 20 text A Simple Plot font plotFont fill brown Set up graph itself canvas c relief raised width 450 height 300 pack w c side top fill x button b text Hello World command exit pack b Hello World in Tk Associative arrays usr bin env tclsh Lists Tk set foo 1 set bar 2 puts eval foo bar Print 3 Command substitution Nifty Tcl Features Tk has been connected to Perl and Python as well Tk a Tcl package provide graphical user interface widgets Tcl Tk may be the easiest way to write a GUI set foo 123 abc bar 1 foo 3 Macro like variable substitution All data is strings incl numbers and lists command argument argument Shell like command syntax Tcl Syntax Wordcount in Tcl Python has the much cleaner syntax and semantics I know which language s programs I d rather maintain More tricks possible in Perl Python more disciplined Probably comparable execution speeds Since become a general purpose scripting language Its syntax is quite simple although rather atypical for a programming language John Ousterhout s Tool Command Language was originally intended …


View Full Document

Columbia COMS W4115 - Scripting Languages

Documents in this Course
YOLT

YOLT

13 pages

Lattakia

Lattakia

15 pages

EasyQL

EasyQL

14 pages

Photogram

Photogram

163 pages

Espresso

Espresso

27 pages

NumLang

NumLang

6 pages

EMPATH

EMPATH

14 pages

La Mesa

La Mesa

9 pages

JTemplate

JTemplate

238 pages

MATVEC

MATVEC

4 pages

TONEDEF

TONEDEF

14 pages

SASSi

SASSi

16 pages

JTemplate

JTemplate

39 pages

BATS

BATS

10 pages

Synapse

Synapse

11 pages

c.def

c.def

116 pages

TweaXML

TweaXML

108 pages

Load more
Loading Unlocking...
Login

Join to view Scripting Languages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Scripting Languages and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?