basicsys@mars~/lec11>cat P2 BEGIN {print "FILENAME="FILENAME;i=1} { if (FILENAME != ARGV[i]) { print ARGV[i], nc, "chars" nc=0; i++ } } { nc = nc + length($0) + 1 } END { print ARGV[i], nc, "chars" print "FILENAME="FILENAME } basicsys@mars~/lec11>cat F1 a b c de fg basicsys@mars~/lec11>cat F2 aa bb cc dd ee fff gg basicsys@mars~/lec11>cat F3 1999 12 ab Fiat 200000 130 cc Subaru basicsys@mars~/lec11>awk -f P2 F1 F2 F3 FILENAME= F1 12 chars F2 32 chars F3 44 chars FILENAME=F3 basicsys@mars~/lec12>cat A1 BEGIN {A[12]="abc";A["abc"]=100;A["de"]="w100z"; for (y in A) { print y,"A["y"]="A[y] } } basicsys@mars~/lec12>awk -f A1 de A[de]=w100z abc A[abc]=100 12 A[12]=abc basicsys@mars~/lec12>cat A2 BEGIN {A[12]="abc";A["abc"]=100;A["de"]="w100z"; print "length of A is:", length(A) for (y in A) {i++} print "i=", i } basicsys@mars~/lec12>awk -f A2 length of A is: 3 i= 3 basicsys@mars~/lec12>cat A3 # examples of usage of substr BEGIN { s="abcdef"; print "substr(s,2,3)="substr(s,2,3) print "substr(s,2)="substr(s,2) print "substr(\"abcdef\",2,3)="substr("abcdef",2,3) } basicsys@mars~/lec12>awk -f A3 substr(s,2,3)=bcd substr(s,2)=bcdef substr("abcdef",2,3)=bcd basicsys@mars~/lec12>cat A4 { if (NF > max_nf) { max_nf = NF } for (i=1 ; i <= NF ; i++ ) { if (length($i) > A[i] ) { A[i] = length($i) } } } END { i=1 while (i <= max_nf) { s = s " " A[i] i++ } s = substr(s,2) print s } basicsys@mars~/lec12>cat F3 abcddd eee ffffff abc g hhhhh abc z a b c d e f g h basicsys@mars~/lec12>awk -f A4 F3 6 5 6 3 1 1 1 1 basicsys@mars~/lec12>cat A5 { print $0 gsub(/abc/,111) print $0 } basicsys@mars~/lec12>awk -f A5 F3 abcddd eee ffffff abc 111ddd eee ffffff 111 g hhhhh abc z g hhhhh 111 z a b c d e f g h a b c d e f g h basicsys@mars~/lec12>cat A6 { print $0 gsub(/abc/,111,$1) print $0 } basicsys@mars~/lec12>awk -f A6 F3 abcddd eee ffffff abc 111ddd eee ffffff abc g hhhhh abc z g hhhhh abc z a b c d e f g h a b c d e f g h basicsys@mars~/lec12>cat A7 BEGIN { s="abcdef" gsub(/[adf]/,111,s) print s } basicsys@mars~/lec12>awk -f A7 111bc111e111 basicsys@mars~/lec12>cat A8 BEGIN { n=split("1aaabc2abc3",A,/a+/) print "A[1]=" A[1] print "A[2]=" A[2] print "A[3]=" A[3] for (i in A) { print "A[" i "]=" A[i] } print "n=" n } basicsys@mars~/lec12>awk -f A8 A[1]=1 A[2]=bc2 A[3]=bc3 A[1]=1 A[2]=bc2 A[3]=bc3 n=3 basicsys@mars~/lec12>cat A9 BEGIN { n=split("1aaabc2abc3",A,/a/) for (i in A) { print "A[" i "]=" A[i] } print "n=" n print "A[1]=" A[1] print "A[2]=" A[2] print "A[3]=" A[3] } basicsys@mars~/lec12>awk -f A9 A[4]=bc2 A[5]=bc3 A[1]=1 A[2]= A[3]= n=5 A[1]=1 A[2]= A[3]= basicsys@mars~/lec12>cat A10 BEGIN {ORS=""} { for (j=1;j <=NF; j++) { A[NR,j]=$j } } END { for (i=1; i<=NR; i++) { for (j=1; j<= NF; j++) { print A[i,j] } print "\n"; } } basicsys@mars~/lec12>cat F3 abcddd eee ffffff abc g hhhhh abc z a b c d e f g h basicsys@mars~/lec12>awk -f A10 F3 abcdddeeeffffffabc ghhhhhabcz abcdefgh basicsys@mars~/lec12>expect -c 'expect "\n" {send "hello world\n"}' hello world basicsys@mars~/lec12>cat A11 expect "\n" {send "Hello world\n"} basicsys@mars~/lec12>expect A11 Hello world basicsys@mars~/lec12>cat A12 #!/usr/bin/expect expect "\n" {send "Hello world\n"} basicsys@mars~/lec12>chmod u+x A12 basicsys@mars~/lec12>A12 Hello world basicsys@mars~/lec12>cat A13 #!/bin/bash echo "enter first number :" read x echo "enter second number :" read y echo "The sum of x and y is $[$x+$y]" basicsys@mars~/lec12>cat A14 #!/usr/bin/expect spawn A13 expect "first number :" send "5\n" expect "second number :" send "8\n" expect basicsys@mars~/lec12>cat A14 #!/usr/bin/expect spawn A13 expect "first number :" send "5\n" expect "second number :" send "8\n" expect basicsys@mars~/lec12>A14 spawn A13 enter first number : 5 enter second number : 8 The sum of x and y is 13 basicsys@mars~/lec12>cat A15 #!/usr/bin/expect expect { "hi" { send "you said hi\n" } "hello" {send "you said hello\n" } timeout {send "There was a timeout\n" } } basicsys@mars~/lec12>A15 hi you said hi basicsys@mars~/lec12>A15 hello you said hello basicsys@mars~/lec12>A15 There was a timeout basicsys@mars~/lec12>A15 abchiddhello you said hi basicsys@mars~/lec12>cat A16 #!/usr/bin/expect set timeout 1 expect { "hi" { send "you said hi\n" } "hia" {send "you said hia\n" } timeout {send "There was a timeout\n" } } basicsys@mars~/lec12>A16 There was a timeout basicsys@mars~/lec12>cat A17 #!/usr/bin/expect set timeout -1 expect { "hi" { send "you said hi\n" } "hia" {send "you said hia\n" } timeout {send "There was a timeout\n" } } basicsys@mars~/lec12>A17 hia you said hi basicsys@mars~/lec12>cat A18 #!/usr/bin/expect spawn telnet rainmaker.wunderground.com expect "Press Return to continue:" {send "\n"} expect "city code" {send "JFK\n"} interact basicsys@mars~/lec12>A18 spawn telnet rainmaker.wunderground.com Trying 38.102.137.140... Connected to rainmaker.wunderground.com (38.102.137.140). Escape character is '^]'. ------------------------------------------------------------------------------ * Welcome to THE WEATHER UNDERGROUND telnet service! * ------------------------------------------------------------------------------ * * * National Weather Service information provided by Alden Electronics, Inc. * * and updated each minute as reports come in over our data feed. * * * * **Note: If you cannot get past this opening screen, you must use a * * different version of the "telnet" program--some of the ones for IBM * * compatible PC's have a bug that prevents proper connection. * * * * comments: jmasters@wunderground.com * ------------------------------------------------------------------------------ Press Return to continue: Press Return for menu or enter 3 letter forecast city code-- JFK Weather Conditions at 01:51 AM EST on 29 Jan 2012 for New York JFK, NY. Temp(F) Humidity(%) Wind(mph) Pressure(in) Weather ======================================================================== 40 40% WEST at 22 30.09 Partly Cloudy Forecast for New York, NY 1243 am EST sun Jan 29 2012 .Overnight...Mostly clear and breezy. Lows in the mid 30s. West winds 15 to 20 mph with gusts up to 35 mph. .Sunday...Sunny. Highs in the mid 40s. West winds 10 to 15 mph. .Sunday night...Mostly cloudy with a slight chance of rain or snow showers in the evening...then mostly clear after midnight. Breezy with lows in the lower 30s. Southwest winds 10 to 15 mph...becoming west 15 to 20 mph after midnight. Gusts up to 30 mph. Chance of precipitation 20 percent. .Monday...Sunny and breezy. Highs in the lower 40s. Northwest winds 15 to 20 mph. Gusts up to 30 mph in the morning. .Monday night...Partly cloudy in the evening...then becoming mostly cloudy. Lows in the mid 30s. Southwest winds 5 to 10 mph. .Tuesday...Partly sunny. Highs in the lower 50s. .Tuesday night...Mostly cloudy. Lows in the lower 40s. .Wednesday...Mostly cloudy. Highs in the mid 50s. Press Return to continue, M to return to menu, X to exit:M .Wednesday night...Mostly cloudy with a 30 percent chance of rain. Lows in the lower 40s. .Thursday...Partly sunny. Highs around 50. .Thursday night...Mostly cloudy. Lows in the upper 30s. .Friday...Mostly cloudy. Highs in the mid 40s. .Friday night...Mostly cloudy. Lows in the mid 30s. .Saturday...Partly sunny. Highs in the lower 40s. CITY FORECAST MENU --------------------------------------------------- 1) Print forecast for selected city 2) Print climatic data for selected city 3) Display 3-letter city codes for a selected state 4) Display all 2-letter state codes M) Return to main menu X) Exit program ?) Help Selection:x Connection closed by foreign host. basicsys@mars~/lec12>cat A18.1 #!/usr/bin/expect spawn telnet rainmaker.wunderground.com expect "Press Return to continue:" {send "\n"} expect "city code" {send "JFK\n"} expect "exit" {send "x\n"} expect basicsys@mars~/lec12>A18.1 spawn telnet rainmaker.wunderground.com Trying 38.102.137.140... Connected to rainmaker.wunderground.com (38.102.137.140). Escape character is '^]'. ------------------------------------------------------------------------------ * Welcome to THE WEATHER UNDERGROUND telnet service! * ------------------------------------------------------------------------------ * * * National Weather Service information provided by Alden Electronics, Inc. * * and updated each minute as reports come in over our data feed. * * * * **Note: If you cannot get past this opening screen, you must use a * * different version of the "telnet" program--some of the ones for IBM * * compatible PC's have a bug that prevents proper connection. * * * * comments: jmasters@wunderground.com * Temp(F) Humidity(%) Wind(mph) Pressure(in) Weather ======================================================================== 40 40% WEST at 22 30.09 Partly Cloudy Forecast for New York, NY 1243 am EST sun Jan 29 2012 .Overnight...Mostly clear and breezy. Lows in the mid 30s. West winds 15 to 20 mph with gusts up to 35 mph. .Sunday...Sunny. Highs in the mid 40s. West winds 10 to 15 mph. .Sunday night...Mostly cloudy with a slight chance of rain or snow showers in the evening...then mostly clear after midnight. Breezy with lows in the lower 30s. Southwest winds 10 to 15 mph...becoming west 15 to 20 mph after midnight. Gusts up to 30 mph. Chance of precipitation 20 percent. .Monday...Sunny and breezy. Highs in the lower 40s. Northwest winds 15 to 20 mph. Gusts up to 30 mph in the morning. .Monday night...Partly cloudy in the evening...then becoming mostly cloudy. Lows in the mid 30s. Southwest winds 5 to 10 mph. .Tuesday...Partly sunny. Highs in the lower 50s. .Tuesday night...Mostly cloudy. Lows in the lower 40s. .Wednesday...Mostly cloudy. Highs in the mid 50s. Press Return to continue, M to return to menu, X to exit: x Connection closed by foreign host. basicsys@mars~/lec12> basicsys@mars~/lec12>cat A19 #!/usr/bin/expect set timeout 60 spawn ftp ftp.cs.huji.ac.il expect "Name" {send "ftp\n"} expect "Password" {send "avi@gmail.com\n"} expect "ftp>" {send "cd cs/pub/doc/OReilly/Nutshell/\n"} expect "ftp>" {send "get perl.tar.gz\n"} expect "ftp>" {send "bye\n"} expect basicsys@mars~/lec12>A19 spawn ftp ftp.cs.huji.ac.il Connected to ftp.cs.huji.ac.il. 220 ProFTPD 1.3.2 Server (ftp.cs.huji.ac.il) [::ffff:132.65.16.9] 500 AUTH not understood 500 AUTH not understood KERBEROS_V4 rejected as an authentication type Name (ftp.cs.huji.ac.il:basicsys): ftp 331 Anonymous login ok, send your complete email address as your password Password: 230 Anonymous access granted, restrictions apply Remote system type is UNIX. Using binary mode to transfer files. ftp> cd cs/pub/doc/OReilly/Nutshell/ 250 CWD command successful ftp> get perl.tar.gz local: perl.tar.gz remote: perl.tar.gz 227 Entering Passive Mode (132,65,16,9,192,39). 150 Opening BINARY mode data connection for perl.tar.gz (68898 bytes) 226 Transfer complete 68898 bytes received in 0.083 seconds (8.1e+02 Kbytes/s) ftp> bye 221 Goodbye. basicsys@mars~/lec12>ls A1 A12 A14.1 A16 A18.1 A20 A5 A8 B8 lec12e.txt perl.tar.gz A10 A13 A14.2 A17 A19 A3 A6 A9 cat oldlec12 A11 A14 A15 A18 A2 A4 A7 A99 F3 P1.1 basicsys@mars~/lec12>cat A20 #!/usr/bin/expect set timeout 60 set x1 [lindex $argv 0] spawn ftp $x1 expect "Name" {send "ftp\n"} expect "Password" {send "avi@gmail.com\n"} expect "ftp>" {send "cd cs/pub/doc/OReilly/Nutshell/\n"} expect "ftp>" {send "get perl.tar.gz\n"} expect "ftp>" {send "bye\n"} expect basicsys@mars~/lec12>A20 ftp.cs.huji.ac.il spawn ftp ftp.cs.huji.ac.il Connected to ftp.cs.huji.ac.il. 220 ProFTPD 1.3.2 Server (ftp.cs.huji.ac.il) [::ffff:132.65.16.9] 500 AUTH not understood 500 AUTH not understood KERBEROS_V4 rejected as an authentication type Name (ftp.cs.huji.ac.il:basicsys): ftp 331 Anonymous login ok, send your complete email address as your password Password: 230 Anonymous access granted, restrictions apply Remote system type is UNIX. Using binary mode to transfer files. ftp> cd cs/pub/doc/OReilly/Nutshell/ 250 CWD command successful ftp> get perl.tar.gz local: perl.tar.gz remote: perl.tar.gz 227 Entering Passive Mode (132,65,16,9,192,36). 150 Opening BINARY mode data connection for perl.tar.gz (68898 bytes) 226 Transfer complete 68898 bytes received in 0.062 seconds (1.1e+03 Kbytes/s) ftp> bye 221 Goodbye.