• No results found

Tcl Awk Xgraph Basics

N/A
N/A
Protected

Academic year: 2021

Share "Tcl Awk Xgraph Basics"

Copied!
73
0
0

Loading.... (view fulltext now)

Full text

(1)

TCL Programming

TCL Programming

Dr. K. Ganesan

Dr. K. Ganesan

Dir

Directo

ector –

r – TIF

TIFAC-

AC-COR

CORE in A

E in Auto

utomot

motive

ive

Infotronics &

Infotronics &

Senior Professor,

Senior Professor,

School of Information Technology and

School of Information Technology and

Engineering

Engineering

VIT University

VIT University

(2)

T

Tcl

cl P

Prro

og

grra

am

mm

miin

ng

g

•• TcTcll (T(Tooool Cl Comommmanand Lad Langnguauagege) is ) is usused ed by by mmilillilionons os off people in the world.

people in the world.

•• It It is is a a lanlanguaguage ge wiwith th a a vevery ry sisimpmple le sysyntantax x anand d it it allallows ows aa very easy integration with other languages.

very easy integration with other languages. •• TTccll wwaas cs crreeaatteed bd by Jy Jhhoonn OOuusstteerrhhoouutt, a, annd td thhee

characteristic of this language are: characteristic of this language are: •• IIt at allllowows s fafasst t dedevveleloopmpmeenntt

•• It It is is cocompmpatatibible le wiwith th mamany ny plplatatfoformrmss •• It It is is flflexexibible le fofor r inintetegrgratatioionn

•• IIt t iis s eeaassy y tto o uussee •• IIt t iis s ffrreeee

(3)

T

Tcl

cl P

Prro

og

grra

am

mm

miin

ng

g

•• TcTcll (T(Tooool Cl Comommmanand Lad Langnguauagege) is ) is usused ed by by mmilillilionons os off people in the world.

people in the world.

•• It It is is a a lanlanguaguage ge wiwith th a a vevery ry sisimpmple le sysyntantax x anand d it it allallows ows aa very easy integration with other languages.

very easy integration with other languages. •• TTccll wwaas cs crreeaatteed bd by Jy Jhhoonn OOuusstteerrhhoouutt, a, annd td thhee

characteristic of this language are: characteristic of this language are: •• IIt at allllowows s fafasst t dedevveleloopmpmeenntt

•• It It is is cocompmpatatibible le wiwith th mamany ny plplatatfoformrmss •• It It is is flflexexibible le fofor r inintetegrgratatioionn

•• IIt t iis s eeaassy y tto o uussee •• IIt t iis s ffrreeee

(4)

Commands evaluation

Commands evaluation

•• E

Eac

ach T

h Tcl

cl co

comm

mman

and c

d cal

all i

l is a

s a se

sent

nten

ence

ce of

of th

the

e fo

forrm

m

::

•• command arg1 arg2 arg3 ...

command arg1 arg2 arg3 ...

•• T

Th

he T

e Tccll e

eva

vallu

ua

atto

or t

r ta

ake

ke e

ea

ach

ch w

wo

orrd o

d of t

f th

hiiss

sentence and evaluates it.

sentence and evaluates it.

•• Af

Afte

ter e

r eva

valu

luat

atio

ion o

n of e

f eac

ach w

h word

ord, t

, the

he fi

firs

rst w

t wor

ord

d

(command) is considered to be a function name

(command) is considered to be a function name

and this function is executed with arguments as

and this function is executed with arguments as

the following words.

the following words.

•• To

To eva

evalu

luate

ate a w

a wor

ord,

d, the

the in

inte

terp

rpre

reter

ter has

has to

to do

do th

the

e

following substitutions in the word string:

(5)

Rules

• If the word is surrounded by " " , this word may contain spaces, but substitution is still applicable inside the quotations. Inside the quotation, there may be spaces and carriage returns.

• If a word is surrounded by { }, this word is unaffected (substitution is thus not applicable on this word). Inside the braces, there may be spaces and carriage returns. Moreover, the { } braces may be nested.

• If a part of the word is surrounded by [ ] , this part is

considered as a command sentence: the text within the brackets is evaluated as a Tcl command and replaced with the result.

• Where substitution is applicable, every string beginning with $ is replaced with the variable represented by this string. This string is ended by a space, a '-' or a ','.

(6)

Examples

• set a "World !"

• In the evaluation of the 3 words 'set', 'a' and '"World !"', no substitution has to be done, only the " " are removed. • The command 'set' is then executed with the parameters

'a' and 'World !'.

• This command tell Tcl to define a new variable 'a' (if not already defined) and to set its value to 'World !'.

• set b "Hello $a"

• Set the variable 'b' to 'Hello World !'.

• Here, the variable substitution has occurred inside the second parameter where the variable 'a' is replaced by its value.

(7)

puts

• set c [string range $b 0 3]

• Set the variable c to 'Hell', which is the 4 first letters of 'Hello World !'.

• In this case, The part between [ ] has been executed as a command

• If we want to break a command sentence in lines we can only do it inside the { } brace or in the " " quotation or we can break the line with a '\' at the end of any break line. • puts

-• Write to a channel

(8)

puts

• Writes the characters given by string to the channel given by channelId .

• ChannelId must be an identifier for an open channel such as a Tcl standard channel (stdout or stderr), the return

value from an invocation of open or socket, or the result of a channel creation command provided by a Tcl extension. The channel must have been opened for output.

• If no channelId is specified then it defaults to stdout.

• Puts normally outputs a newline character after string , but this feature may be suppressed by specifying the

-nonewline switch.

• Tcl buffers output internally, so characters written with puts may not appear immediately on the output file or device; • Tcl will normally delay output until the buffer is full or the

channel is closed.

• We can force output to appear immediately with the flush command.

(9)

Examples

• Write a short message to the console (or wherever stdout is directed):

• puts "Hello, World!"

• Print a message in several parts: • puts -nonewline "Hello, "

• puts "World!"

• Print a message to the standard error channel: • puts stderr "Hello, World!“

• Comment:

• The sign # start a commented line that is not part of the program, so the Tcl interpreter will not execute this line. • # I am not executed

(10)

File handling

• To create a file, one has to give it a name, say “filename”, and to assign a pointer to it that will be used within the Tcl program in order to relate to it, say “file1”.

• This is done by the command: set file [open filename w]. • The command puts is used for printing an output.

• If we want to print in to a file, we type puts $file1 “text”. • Tabulating is done by inserting \t.

• For example, if avariable, say x, has the value 2 and we type puts  $file1 “x $x” then this will print a line into the file whose name is “filename” with two elements: “x” and “2” separated by a tabulator space.

• EXAMPLE

• Append a log message to a file:

• set chan [open my.log a] #(here my.log is a file and “a” is append in file)

• set timestamp [clock format [clock seconds]] • puts $chan "$timestamp - Hello, World!" • close $chan

(11)

Expr

• A mathematical operation is done using the expression command. • For example, if we wish to assign to a variable x the sum of some

variables a and b , we should write • “set x [expr $a+$b] 

• In Tcl variables are not typed , so a variable can be a string or an integer depending on the value we assign to it.

• For example, assume that we want to print the result of division 1/60. If we write

• puts “[expr 1/60]” the the result will be 0! To have a correct result, we need to indicate that we do not work with integer, and should type

• puts “[expr 1.0/60.0]” 

• When the expression parser encounters a mathematical function such as pow($a,$b), it replaces it with a call to an ordinary Tcl function in the tcl::mathfunc namespace.

• The processing of an expression such as: • set a 43

• set b 27

(12)

If

• SYNOPSIS

• if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN ?

• DESCRIPTION

• The if command evaluates expr1 as an expression (in the same way that expr evaluates its argument).

• The value of the expression must be a boolean (a numeric value, where 0 is false and anything is true, or a string value such as true or yes for true and false or no for false);

• if it is true then body1 is executed by passing it to the Tcl interpreter. • Otherwise expr2 is evaluated as an expression and if it is true then

body2 is executed, and so on.

• If none of the expressions evaluates to true then bodyN is executed. • The then and else arguments are optional "noise words" to make

the command easier to read.

• There may be any number of elseif clauses, including zero. • BodyN may also be omitted as long as else is omitted too.

• The return value from the command is the result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN .

(13)

• A simple conditional: • if {$vbl == 1} { puts "vbl is one" } • With an else-clause: • if {$vbl == 1} { • puts "vbl is one" • } else {

• puts "vbl is not one" • }

• With an elseif-clause too: • if {$vbl == 1} {

• puts "vbl is one" • } elseif {$vbl == 2} { • puts "vbl is two" • } else {

• puts "vbl is not one or two" • }

• Remember, expressions can be multi-line, but in that case it can be a good idea to use the optional then keyword for clarity:

• if { $vbl == 1 || $vbl == 2 || $vbl == 3} then { • puts "vbl is one, two or three"

(14)

For loop

• Loops have the following form: • for {set i 0} {$i < 5} {incr i} {

• puts "x is $x" }

• In this example the command in the loop will execute five times.

• After the for the {set i 0} declares the variable i that will be used as the counter of the loop and initializes it to 0.

• The second part between {} is the continuation condition of the loop, it says “do the loop while the counter i is less than 5”.

• The last part of the statement is for declaring the changing in the counter variable, in this case we increment i one by one, but we can also decrement it or use any mathematical

expressions for increment or decrement the counter instead. • for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} {

(15)

Foreach

• The foreach command implements a loop where the loop variable(s) take on values from one or more lists. • In the simplest case there is one loop variable, varname ,

and one list, list , that is a list of values to assign to varname .

• set values {1 3 5 7 2 4 6 8} # Odd numbers first, for fun!

• puts "Value\tSquare\tCube" # Neat-looking header • foreach x $values { # Now loop and print... • puts " $x\t [expr {$x**2}]\t [expr {$x**3}]"

(16)

While loop

• The while command evaluates test as an expression (in the same way that expr evaluates its argument).

• The value of the expression must a proper boolean value; if it is a true value then body is executed by passing it to the Tcl interpreter. • Once body has been executed then test is evaluated again, and the

process repeats until eventually test evaluates to a false boolean value.

• Continue commands may be executed inside body to terminate the current iteration of the loop, and break commands may be executed inside body to cause immediate termination of the while command. • The while command always returns an empty string.

• set x 0

• while {$x<10} { • puts "x is $x" • incr x

(17)

Exec

• Execution of a unix command is done by

typing “exec” command.

• For example, we may wants to initiate the

display of a curve whose data are given in

a two column file named “data” within the

simulation.

• This can be done using xgraph command

and will be written as:

(18)

Defining a list

• A list in Tcl consists of a string whose items are separated by white space. For example, this is a list,

• A B C D E

• To assign a list to a variable we can do this, • set list1 {A B C D E}

• or this,

• set list1 [list A B C D E] 

• The second method is better because the use of the list command makes it much clearer that a list is being created. To print a raw list to the console we use the puts command,

• puts "The contents of list1 are: $list1"  • Embedded Lists

• It is possible for one list to have another list embedded within it, for example,

• set list1 [list A B [list 1 2 3] C D E] 

• A list may also contain consecutive lists, for example, • set list1 [list DOG CAT MULE] 

• set list2 [list dog cat mule]  • set list3 [list $list1 $list2] 

(19)

Llength and lindex

• Count the number of elements in a list

set list1 {A B C D E}

llength $list1 # result is 5

lindex

• Retrieve an element from a list

set list1 {A B C D E}

lindex $list1 0 # result is A

lindex $list1 2 # result is C

(20)

Procedures

• Tcl allows to create procedures. They can return some values in which case they contain a “return” command.

• The general form of a procedure which we name “red” is • proc red {par1 par2 ...} {

• global var1 var2 ... • <commands>

• return $something • }

• The procedure receives some parameters that can be objects, files or variables.

• In our case these are named par1, par2, etc. These parameters will be used within the procedures with these names.

• The procedure is called by typing red x y ... where the value of x and y will be used by the procedure for par1 and par2.

• If par1 and par2 are changed within the procedure, this will not affect the values of x and y.

• On the other hand, if we wish the procedure to be able to affect directly variables external to it, we have to declare these variables as “global”. In the above example these variables are var1 and var2.

(21)

Creating and calling a procedure

• # Create a procedure • proc test {} { • set a 43 • set b 27 • set c [expr $a + $b]

• set d [expr [expr $a - $b]*$c] • puts “c=$c d=$d”

• for {set k 1} {$k < 10} {incr k} {

• if {$k<5} { puts “k<5, pow=[expr pow($d,$k)] • } else { puts “k>5, mod=[expr $d%$k]”

• } • } • }

• # Calling the procedure • test

(22)
(23)

OTcl Programming

• The reserved word “Class” followed of the name of the

class is used to declare a new class in OTcl.

• The methods of classes are declared using the word

“instproc” preceded of the name of the class and

followed of the name of the method and its parameters.

• The method “init” is the constructor of the class.

• The variable “self” is a pointer to an object itself, like

“this” in C++ or Java.

• To declare an instance variable OTcl uses the word

“instvar”.

• The word “-superclass” is used for declaring that a cla

inherits from another one, in the below example the kid

class inherits from mom class.

(24)

Example

• # Add a member function call “greet” • Class mom

• mom instproc greet {} { • $self instvar age_ 

• puts “$age_ year old mom say: How are you doing?” • }

• # Create a child class of “mom” called “kid” • # and override the member of function “greet” • Class kid – superclass mom

• kid instproc greet {} { • $self instvar age_ 

• puts “$age_ year old kid say: What’s up dude?” • }

(25)

Example

• # Create a mom and a kid object, set each age • set a [new mom]

• $a set age_ 45 • set b [new kid] • $b set age_ 15

• # Calling member function “greet” of each object • $a greet

• $b greet

• The result is:

• 45 year old mom say: • How are you doing? • 15 year old kid say: • What’s up dude?”

(26)

Awk Programming

Dr. K. Ganesan

Director – TIFAC-CORE in Automotive

Infotronics &

Senior Professor, School of Information

Technology and Engineering

(27)

Introduction

Introduction

•• A

Awk

wk ut

utililit

ity

y iis

s po

powe

werf

rful

ul da

data

ta m

man

anip

ipul

ulat

atio

ion

n

 /scripting programming language (In fact based

 /scripting programming language (In fact based

on the C programming Language).

on the C programming Language).

•• O

One

ne ca

can

n us

use

e aw

awkk tto

o ha

hand

ndle

le co

com

mpl

plex

ex ta

task

sk su

such

ch

as calculation, database handling, report

as calculation, database handling, report

creation etc.

creation etc.

•• C

Co

on

nssiid

de

er f

r fo

ollllo

ow

wiin

ng t

g te

exxt d

t da

ata

tab

ba

asse f

e fiille -

e - iin

nve

ven

n..tr

tr

• 1

• 1.. P

Pe

en

n

5

5

2

20

0..0

00

0

•• 2

2.. R

Ru

ub

bb

be

err

1

10

0

2

2..0

00

0

•• 3

3.. P

Pe

en

ncciill

3

3

3

3..5

50

0

•• 4

4.. C

Co

occkk

2

2

4

45

5..5

50

0

(28)

Introduction

Introduction

•• In aIn abovbove fie file, le, fiefields lds arare Sre Sr.N.No, Po, Proroduductct, Qt, Qty, y, UnUnit Pit Priricece.. •• FiFieleld id is ts the he smsmalallelest st elelememenent of t of anany ry rececorord.d.

•• EaEacch fh fiieleld hd has as itits os owwn an attttriribubutetes.s.

•• FoFor e.r e.g. g. TaTake ke QtQty fy fieield. ld. QtQty. y. fifieldeld’s a’s attttribributute is e is ititss numerical value.

numerical value.

•• CoCollllecectition on of of fifielelds ds is is knknowown an as rs rececorord.d. •• SoSo

1. Pen 5 20.00 ----> Is a Record. 1. Pen 5 20.00 ----> Is a Record.

•• CColollelectctioion on of rf rececorords ds iis ks knonowwn an ass database file database file ..

•• In aIn abovbove tee text xt dadatabtabase ase filfile eae each ch fifield eld is is seseparparatated ued usisingng space (or tab character) and record is separated using space (or tab character) and record is separated using new-line character (i.e. each record is finished at the end new-line character (i.e. each record is finished at the end of a line ).

of a line ).

•• In In awawk, k, fiefields lds arare ae accccesessesed ud usising a ng a spspececial ial vavaririablable.e. •• FoFor er e.g. .g. In In aboabove ve dadatabtabasase $e $1, $1, $2, 2, $3$3, $, $4 r4 resespepectctivivelyely

represents Sr.No, Product, Qty, Unit Price fields represents Sr.No, Product, Qty, Unit Price fields

(29)

Example

Example

•• NoNow enw enteter thr the foe follllowowining sig simpmple ale awkwk prprogograram/ m/ cocommmmanand at sd at shehellll prompt:

prompt: $ awk

$ awk '{ print $'{ print $1 $2 "--> 1 $2 "--> Rs." $3 * Rs." $3 * $4 }' inv$4 }' invent.trent.tr 1.Pen--> Rs.100  1.Pen--> Rs.100  2.Pencil--> Rs.20  2.Pencil--> Rs.20  3.Rubber--> Rs.10.5  3.Rubber--> Rs.10.5  4.Cock--> Rs.91 4.Cock--> Rs.91

•• AbAbovove ae awkwk prprogograram/m/cocommmmanand cd can an be be exexplplaiainened ad as fs folollolowsws •• '{ print $1 $2 "--> Rs." $3 * $4 }'{ print $1 $2 "--> Rs." $3 * $4 }

•• HeHere re prprinint ct comommamand nd is is usused ed to to prprinint tt the he cocontntentents os of vf varariaiablbles es oror tetextxt enclosed in " text ".

enclosed in " text ".

•• HeHere re $1$1, , $2$2, , $3$3,$,$4 4 arare ae all ll ththe e spspececiaial l vavaririabableles.s. •• TThe he vvaaririaabbllees $s $11, $, $22,, eettc cc coonnttaaiin vn valalue ue oof ff fiieelldd..

•• FiFinalnally ly we cwe can dan dirirecectltly dy do to the che calalculculatatioion un usising ng $3 $3 * $4 * $4 i.i.e.e. multiplication of third and fourth fields in database.

multiplication of third and fourth fields in database. •• NNootte e tthhaatt "--> Rs.""--> Rs." is a string which is printed as its.is a string which is printed as its.

(30)

Example

• Now enter the following simple awk program/ command at shell prompt:

$ awk '{ print $1 $2 "--> Rs." $3 * $4 }' invent.tr 1.Pen--> Rs.100 

2.Pencil--> Rs.20  3.Rubber--> Rs.10.5  4.Cock--> Rs.91

• Above awk program/command can be explained as follows • '{ print $1 $2 "--> Rs." $3 * $4 }

• Here print command is used to print the contents of variables or text enclosed in " text ".

• Here $1, $2, $3,$4 are all the special variables. • The variables $1, $2, etc contain value of field.

• Finally we can directly do the calculation using $3 * $4 i.e. multiplication of third and fourth fields in database.

(31)

Sample outputs

• Type following awk program at the shell prompt, $ awk '{ print $2 }' invent.tr

Pen  Pencil  Rubber  Cock 

• To print second and fourth fields from file give following command:

$awk '{ print $2 $4}' invent.tr Pen20.00

Pencil2.00 Rubber3.50 Cock45.50

• $0 is a special variable of awk , which prints the entire record: $ awk '{ print $0 }' invent.tr

1. Pen 5 20.00  2. Pencil 10 2.00  3. Rubber 3 3.50  4. Cock 2 45.50 

(32)

Arithmetics with Awk

• One can do the arithmetic with awk as follows: • $ cat > math.awk { print $1 " + " $2 " = " $1 + $2 print $1 " - " $2 " = " $1 - $2 print $1 " / " $2 " = " $1 / $2 print $1 " x " $2 " = " $1 * $2 print $1 " mod " $2 " = " $1 % $2 }

• Run the awk program as follows: • $ awk -f math.awk 20 3 20 + 3 = 23 20 - 3 = 17 20 / 3 = 6.66667 20 x 3 = 60 20 mod 3 = 2 (Press CTRL + D to terminate)

(33)

Explanation

• In above program print $1 " + " $2 " = " $1

+ $2, statement is used for addition

purpose.

• Here $1 + $2, means add (+) first field with

second field.

• Same way one can do - (subtraction ), *

(Multiplication), / (Division), % (modular

used to find the remainder of division

operation).

(34)

User defined variables in Awk

• One can define a variable in awk program, as follows: • $ cat > math1.awk

{

no1 = $1 no2 = $2

ans = $1 + $2

print no1 " + " no2 " = " ans }

• Run the program as follows $ awk -f math1.awk

1 5 

(35)

Code explanation

• In the above program, no1, no2, ans all are user defined variables.

• Value of first and second fields are assigned to no1, no2 variable respectively and the addition to ans variable.

• Value of variable can be printed using print statement as, print no1 " + " no2 " = " ans.

• Note that print statement prints whatever enclosed in double quotes (" text ") as it is.

• If string is not enclosed in double quotes it is treated as a variable.

• Also the above two programs take the input from stdin (Keyboard) instead of a file.

(36)

• Now try the following awk program and note down its output. • $ cat > bill.awk { total = $3 * $4 recno = $1 item = $2

print recno item " Rs." total }

• Run it as:

$ awk -f bill.awk invent.tr 1.Pen Rs.100 

2.Pencil Rs.20  3.Rubber Rs.10.5  4.Cock Rs.91

• Here we are printing the total price of each product (By multiplying third field with fourth field).

(37)

Printing the output

• Following program prints total price of each

product as well as the Grand total of all product

in the bracket.

• $ cat > bill1.awk

{

total = $3 * $4

recno = $1

item = $2

gtotal = gtotal + total

print recno item " Rs." total " [Total Rs." gtotal "]

"}

(38)

Code

• Run the above awk program as follows: $ awk -f bill1 inven

1.Pen Rs.100 [Total Rs.100]  2.Pencil Rs.20 [Total Rs.120] 

3.Rubber Rs.10.5 [Total Rs.130.5]  4.Cock Rs.91 [Total Rs.221.5] 

• In this program, gtotal variable holds the grand total.

• It adds the total of each product as gtotal = gtotal + total. • Finally this total is printed with each record in the

bracket.

• But there is one problem with our script, Grand total mostly printed at the end of all record.

(39)

• To solve this problem we have to use special BEGIN and END Patterns of awk. First take the example,

• $ cat > bill2.awk BEGIN {

print "---"

print "Bill for the 4-March-2001. " print "By Vivek G Gite. "

print "---" } { total = $3 * $4 recno = $1 item = $2 gtotal += total

print recno item " Rs." total }

END {

print "---" print "Total Rs." gtotal

print "===========================" }

(40)

Sample output

• Run it as

$awk -f bill2.awk invent.tr --- 

Bill for the 4-March-2001. By Vivek G Gite. ---  1.Pen Rs.100  2.Pencil Rs.20  3.Rubber Rs.10.5  4.Cock Rs.91 ---  Total Rs.221.5  =============== 

(41)

• Now the grand total is printed at the end.

• In above program BEGIN and END patters are used.

• BEGIN actions before the first line (Record) has been read from database file.

• Use BEGIN pattern to set value of variables, to print heading for report etc.

• General syntax of BEGIN is as follows BEGIN {

action 1 action 2 action N }

• END instruct awk, that perform END actions after reading all lines (RECORD) from the database file.

• General syntax of END is as follows: • END {

action 1 action 2 action N }

• In our example, BEGIN is used to print heading and END is used print grand total.

(42)

Printf statement

• Next example shows the use of special printf statement • $ cat > bill3.awk

BEGIN {

printf "Bill for the 4-March-2001.\n" printf "By Vivek G Gite.\n"

printf "---\n" } { total = $3 * $4 recno = $1 item = $2 gtotal += total

printf "%d %s Rs.%f\n", recno, item, total

#printf "%2d %-10s Rs.%7.2f\n", recno, item, total }

END {

printf "---\n" printf "Total Rs. %f\n" ,gtotal

#printf "\tTotal Rs. %7.2f\n" ,gtotal

printf "===========================\n" }

(43)

Sample output

• Run it as follows:

$ awk -f bill3.awk invent.tr Bill for the 4-March-2001. By Vivek G Gite. ---  1 Pen Rs.100.000000  2 Pencil Rs.20.000000  3 Rubber Rs.10.500000  4 Cock Rs.91.000000  ---  Total Rs. 221.500000  =============== 

• In above example printf statement is used to print formatted output of the variables or text.

(44)

• General syntax of printf as follows: • printf "format" ,var1, var2, var N

printf "Hello"

printf "Hello World\n"

• In last example \n is used to print new line.

• It’s Part of escape sequence. The following may be also used:

 \t for tab

 \a Alert or bell

 \" Print double quote etc

• For e.g. printf "\nAn apple a day, keeps away\t\t\tDoctor\n\a\a"

It will print text on new line as :

An apple a day, keeps away Doctor 

• Notice that twice the sound of bell is produced by \a\a. • To print the value of decimal number use %d as format

specification code followed by the variable name. • For e.g. printf "%d" , no1

(45)
(46)

If condition in Awk

• General syntax of if condition is as follows: if ( condition ) { Statement 1 Statement 2 Statement N // if condition is TRUE } else { Statement 1 Statement 2 Statement N // if condition is FALSE }

(47)

• $ awk > math2.awk BEGIN {

myprompt = "(To Stop press CTRL+D) > "

printf "Welcome to MyAddtion calculation awk program v0.1\n" printf "%s" ,myprompt } { no1 = $1 op = $2 no2 = $3 ans = 0 if ( op == "+" ) { ans = $1 + $3 printf "%d %c %d = %d\n" ,no1,op,no2,ans printf "%s" ,myprompt } else {

printf "Opps!Error I only know how to add.\nSyntax: number1 + number2\n" printf "%s" ,myprompt } } END { printf "\nGoodby \n" }

(48)

Sample output

• Run it as follows (Give input as 5 + 2 and 3 - 1

which is shown in bold words)

$awk -f math2.awk

Welcome to MyAddtion calculation awk program 

v0.1

(To Stop press CTRL+D) > 

5 + 2 

5 + 2 = 7 

(To Stop press CTRL+D) > 

3 - 1

Opps!Error I only know how to add.

Syntax: number1 + number2 

(To Stop press CTRL+D) > 

Goodby 

(49)

Loops in Awk

• For loop and while loop are used for looping purpose in awk.

Syntax: 

for (expr1; condition; expr2) {

Statement 1 Statement 2 Statement N }

• Statement(s) are executed repeatedly UNTIL the condition is true.

• BEFORE the first iteration, expr1 is evaluated.

• This is usually used to initialize variables for the loop. • AFTER each iteration of the loop, expr2 is evaluated. • This is usually used to increment a loop counter.

(50)

• $ cat > for1.awk • BEGIN{

printf "Press ENTER to continue with for loop example \n" }

{

sum = 0 i = 1

for (i=1; i<=10; i++) {

sum += i; # sum = sum + i }

printf "Sum for 1 to 10 numbers = %d \nGoodby!\n\n", sum exit 1

}

• Run it as follows: $ awk -f for1.awk

Press ENTER to continue with for loop example from  • Sum for 1 to 10 numbers = 55 

Goodby 

• Above for loops prints the sum of all numbers between 1 to 10, it does use very simple for loop to achieve this.

• It takes number from 1 to 10 using i variable and add it to sum variable as sum = previous sum + current number (i.e. i).

(51)

While loop

• while loop as follows:

Syntax: 

while (condition)

{

statement1

statement2

statementN

• // Continue as long as given condition is TRUE

}

• While loop will continue as long as given

condition is TRUE.

(52)

Sample code

• $ cat > while_loop.awk { no = $1 remn = 0 while ( no > 1 ) { remn = no % 10 no /= 10 printf "%d" ,remn }

printf "\nNext number please (CTRL+D to stop):"; }

• Run it as

$awk -f while_loop.awk • 654

456

Next number please(CTRL+D to stop):587 785

Next number please(CTRL+D to stop):

(53)

Arrays

• Awk has arrays. However, under awk, it's customary to start array indices at 1, rather than 0.

• myarray[1]="jim" • myarray[2]=456

• When awk encounters the first assignment, myarray is created and the element myarray[1] is set to "jim".

• Iterating over arrays

• Once defined, awk has a handy mechanism to iterate over the elements of an array, as follows:

• for ( x in myarray ) { • print myarray[x]

(54)

Code details

• This code will print out every element in the array myarray.

• When we use this special "in" form of a for loop, awk will assign every existing index of myarray to x (the loop control variable) in turn, executing the loop's code block once after each assignment. • While this is a very handy awk feature, it does have one drawback

--when awk cycles through the array indices, it doesn't follow any particular order.

• That means that there's no way for us to know whether the output of above code will be:

• jim • 456 • or • 456 • Jim

(55)

Introduction to Xgraph

Dr. K. Ganesan

Director – TIFAC-CORE in

Automotive Infotronics &

Senior Professor, School of

Information Technology and

Engineering

(56)

Introduction

• The xgraph program draws a graph on a display

device.

• The input data can be read from either data files

or from standard input if no files are specified.

• It can display up to 64 independent data sets

using different colors and/or line styles for each

set.

• It annotates the graph with a title, axis labels,

grid lines or tick marks, grid labels, and a

(57)

Options

• There are options to control the appearance of most components of the graph.

• A data set consists of an ordered list of points of the form “directive X Y”.

• For directive “draw”, a line will be drawn between the previous point and the current point.

• Specifying a “move” directive tells xgraph not to draw a line between the points.

• “draw” is the default directive.

• The name of a data set can be specified by enclosing the name in double quotes.

(58)

Window interface

• The interface used to specify the size and

location of this window depends on the window

manager currently in use.

• Once the window has been opened, all of the

data sets will be displayed graphically with a

legend in the upper right corner of the screen.

• Xgraph also presents three control buttons in the

upper left corner of each window:

(59)

xgraph command options

• -geometry WxH (Geometry)

• Specifies the initial size and location of the xgraph window.

• -bar (BarGraph)

• Specifies that vertical bars should be drawn from the

data points to a base point which can be specified with -brb.

• -brb <base> (BarBase)

• This specifies the base for a bar graph. By default, the base is zero.

• -brw <width> (BarWidth)

• This specifies the width of bars in a bar graph. The

amount is specified in the user’s units. By default, a bar one pixel wide is drawn.

(60)

Options

• -fitx 

• Translate and scale the x data from all datasets to fit [0. . . 1]. • -fity 

• Translate and scale the y data from all datasets to fit [0. . . 1]. • -fmtx <printf-format> -fmty <printf-format> 

• Use the format specified to generate the legends for the x or y axis.

• -bb (BoundBox)

• Draw a bounding box around the data region.

• This is very useful if you prefer to see tick marks rather than grid lines.

(61)

options

• -bd <color> (Border)

• This specifies the border color of the xgraph window. • -bg <color> (Background)

• Background color of the xgraph window. • -bw <size> (BorderSize)

• Border width (in pixels) of the xgraph window. • -fg <color> (Foreground)

• Foreground color. This color is used to draw all text and the normal grid lines in the window.

• -gw (GridSize)

(62)

Font options

• -gs (GridStyle)

• Line style pattern of normal grid lines. • -lf <fontname> (LabelFont)

• Label font. All axis labels and grid labels are drawn using this font.

• A font name may be specified exactly (e.g. ”9x15” or ”-*-courier-bold-rnormal-*- 140-*”) or in an abbreviated form. • The family is the family name (like helvetica) and the

size is the font size in points (like 12). The default for this parameter is ”helvetica-12”.

• -lnx (LogX)

• Specifies a logarithmic X axis. Grid labels represent powers of ten.

• -lny (LogY)

• Specifies a logarithmic Y axis. Grid labels represent powers of ten.

(63)

Options - lines

• -lw width (LineWidth)

• Specifies the width of the data lines in pixels.

The default is zero.

• -lx <xl,xh> (XLowLimit, XHighLimit)

• This option limits the range of the X axis to the

specified interval. This (along with -ly) can be

used to ”zoom in” on a particularly interesting

portion of a larger graph.

• -ly <yl,yh> (YLowLimit, YHighLimit)

• This option limits the range of the Y axis to the

specified interval.

(64)

Options - Legends

• -m (Markers)

• Mark each data point with a distinctive marker. There are eight distinctive markers used by xgraph. These markers are assigned uniquely to each different line style on black and

white machines and varies with each color on color machines. • -M (StyleMarkers)

• Similar to -m but markers are assigned uniquely to each eight consecutive data sets (this corresponds to each different line style on color machines).

• -nl (NoLines)

• Turn off drawing lines. When used with -m, -M, -p, or -P this can be used to produce scatter plots. When used with -bar, it can be used to produce standard bar graphs.

• -ng (NoLegend)

• Turn off drawing Legends. Can be used to increase the drawing area.

(65)

options

• -t <string> (TitleText)

• Title of the plot. This string is centered at the top of the graph. • -tf <fontname> (TitleFont)

• Title font. This is the name of the font to use for the graph title. A font name may be specified exactly (e.g. ”9x15” or ”-*-courier-bold-r-normal-*- 140-*”) or in an abreviated form.

• The family is the family name (like helvetica) and the size is the font size in points (like 12). The default for this parameter is ”helvetica-18”.

• -x <unitname> (XUnitText)

• This is the unit name for the X axis. Its default is ”X”. • -y <unitname> (YUnitText)

(66)

Example

• Store this data in a file called “DataFile.txt” • 1 4 • 2 6 • 3 5 • 4 9 • 5 11 • 6 15 • 7 3 • 8 6 • 9 16 • 10 14 • 11 11 • 12 2 • 13 8 • 14 5

(67)
(68)

xgraph DataFile.txt –geometry 400x400 –bar –brb

2 –brw 0.5 –tk –bb –nl –bg white –t

(69)

xgraph DataFile.txt –geometry 400x400 –tk –bb – 

lw 5 –lnx -lny –bg white –x “Log_X” –y “Log_Y”

(70)

xgraph DataFile.txt –geometry 400x400 –tk –bb – 

M –fitx -fity –bg white –x “Fit_X” –y “Fit_Y”

(71)

Xgraph DataFile.txt –bb –fg blue –gw 2 –gs

1 –lf “9x15” –bg white –t “grid style” – 

(72)

Xgraph DataFile.txt –ng –lx 11,14 –ly

2,10 –t “ZOOM” –geometry 800x400

(73)

Xgraph DataFile.txt DataFile2.txt –bg white

 –tk –bb –m -M –t “Marker” –geometry

References

Related documents

Brachiosaurus (BRAK-ee-uh-SAWR-us) 75 feet long and weighed 75 tons.. One of

This review focuses on an update of the / hydrolase fold family of mammalian epoxide hydrolases and their role in xenobiotic metabolism, but will also summarise the functions

En el caso de Vip Dating Perú, la evidencia física esencial está constituida por la imagen de Roxana Tutaya, así como las fotos que se exhiben de cada

God is good all the time He put a song of praise In this heart of mine God is good all the time Through the darkest night His light will shine!. God

(2 pts) Consider a portfolio consisting of the following four European options with the same expiration date T on the underlying asset S:.. • long one call with strike 40, • long

4) An object will sink in water if its density is greater than the density of water. As the volume of the box increases, the pressure increases

The train set is made up of two locomotives with a weight of 95 metric tons per locomotive and 45 flat cars with trailers behind (65 metric tons each). a) Find the maximum speed of

The ________ function returns Boolean True value if all the keys in the dictionary are True else returns False.. Predict the output of the