Tcl Awk Xgraph Basics
Short Description
TCL Programming - Basics and Execution Awk Execution Xgraph Commands...
Description
TCL Programming Dr. K. Ganesan Directo Directorr – TIFACTIFAC-COR CORE E in Auto Automot motive ive Infotronics & Senior Professor, School of Information Technology and Engineering VIT University
Tcl Programming • Tcl Tcl (Too (Tooll Com Comm mand and Lang Langua uage ge)) is used used by milli illion ons s of of people in the world. • It is a langua language ge with with a very very simp simple le synta syntax x and and it allows allows a very easy integration with other languages. • Tcl was cr created by by Jh Jhon Ousterhout, an and th the characteristic of this language are: • It al allows ows fas fast dev develo elopme pment • It is comp compat atib ible le with with many many plat platfo form rms s • It is flex flexib ible le for for inte integr grat atio ion n • It is easy to use • It is free
Tcl Programming • Tcl Tcl (Too (Tooll Com Comm mand and Lang Langua uage ge)) is used used by milli illion ons s of of people in the world. • It is a langua language ge with with a very very simp simple le synta syntax x and and it allows allows a very easy integration with other languages. • Tcl was cr created by by Jh Jhon Ousterhout, an and th the characteristic of this language are: • It al allows ows fas fast dev develo elopme pment • It is comp compat atib ible le with with many many plat platfo form rms s • It is flex flexib ible le for for inte integr grat atio ion n • It is easy to use • It is free
Commands evaluation • Each ach Tcl Tcl comm comman and d cal calll is is a sent senten ence ce of the the for form : • command arg1 arg2 arg3 ... • The Tc Tcl eval valuator ta take each word of of th this sentence and evaluates it. • Afte Afterr eva evalu luat atio ion n of of eac each h word word,, the the firs firstt wor word d (command) is considered to be a function name and this function is executed with arguments as the following words. • To evalu evaluate ate a wor word, d, the inte interp rpre reter ter has to do the the following substitutions in the word string:
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 ','.
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.
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 • SYNOPSIS: puts ?-nonewline? ?channelId ? string
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.
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
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
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 set powr = [expr pow($a,$b)]
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 .
• • • • • • • • • • • • • • • • • • • •
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" }
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 Rs.100 2.Pencil--> Rs.20 3.Rubber--> Rs.10.5 4.Cock--> Rs.91 Abov Above e awk awk prog progra ram/ m/co comm mman and d can can be expl explai aine ned d as as fol follo lows ws '{ print $1 $2 "--> Rs." $3 * $4 } Here Here prin printt com comma mand nd is used used to prin printt the the cont content ents s of of var varia iabl bles es or text text enclosed in " text ". Here Here $1, $1, $2, $2, $3,$ $3,$4 4 are are all all the the spec specia iall vari variab able les. s. The varia riables $1 $1, $2 $2, etc co contain val value ue of fifield. Final Finally ly we can can dir direc ectl tly y do do the the cal calcul culat atio ion n usi using ng $3 * $4 i.e. i.e. multiplication of third and fourth fields in database. Note that "--> Rs." is a string which is printed as its.
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. Note that "--> Rs." is a string which is printed as its.
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
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)
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).
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 1 + 5 = 6
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.
• 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).
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 "] "}
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.
• •
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 "===========================" }
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 ===============
• • • • •
• • •
•
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.
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" }
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.
• 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 • It will print the value of no1.
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 } • Above if syntax is self explontary.
•
$ 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" }
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
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.
• •
• • • •
$ cat > for1.awk BEGIN{ printf "Press ENTER to continue with for loop example \n" } { sum = 0 i=1 for (i=1; i 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): Here user enters the number 654 which is printed in reverse order i.e. 456.
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] • }
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
Introduction to Xgraph Dr. K. Ganesan Director – TIFAC-CORE in Automotive Infotronics & Senior Professor, School of Information Technology and Engineering VIT University, Vellore – 632 014
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 legend.
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.
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: • Hardcopy, Close and About
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 (BarBase) • This specifies the base for a bar graph. By default, the base is zero. • -brw (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.
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 -fmty 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. • • • • • •
options -bd (Border) This specifies the border color of the xgraph window. -bg (Background) Background color of the xgraph window. -bw (BorderSize) Border width (in pixels) of the xgraph window. -fg (Foreground) Foreground color. This color is used to draw all text and the normal grid lines in the window. • -gw (GridSize) • Width, in pixels, of normal grid lines. • • • • • • • •
Font options • • • • • • • • • •
-gs (GridStyle) Line style pattern of normal grid lines. -lf (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.
Options - lines • -lw width (LineWidth) • Specifies the width of the data lines in pixels. The default is zero. • -lx (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 (YLowLimit, YHighLimit) • This option limits the range of the Y axis to the specified interval.
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.
options • • • • • • • • •
-t (TitleText) Title of the plot. This string is centered at the top of the graph. -tf (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-rnormal-*- 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 ”helvetica18”. -x (XUnitText) This is the unit name for the X axis. Its default is ”X”. -y (YUnitText) This is the unit name for the Y axis. Its default is ”Y”.
Example • • • • • • • • • • • • • • •
Store this data in a file called “DataFile.txt” 14 26 35 49 5 11 6 15 73 86 9 16 10 14 11 11 12 2 13 8 14 5
xgraph DataFile.txt –geometry 400x400
xgraph DataFile.txt –geometry 400x400 –bar –brb 2 –brw 0.5 –tk –bb –nl –bg white –t “Example_Xgraph” –x “Xaxis” –y “Yaxis”
xgraph DataFile.txt –geometry 400x400 –tk –bb – lw 5 –lnx -lny –bg white –x “Log_X” –y “Log_Y”
xgraph DataFile.txt –geometry 400x400 –tk –bb – M –fitx -fity –bg white –x “Fit_X” –y “Fit_Y”
Xgraph DataFile.txt –bb –fg blue –gw 2 –gs 1 –lf “9x15” –bg white –t “grid style” – geometry 800x400
Xgraph DataFile.txt –ng –lx 11,14 –ly 2,10 –t “ZOOM” –geometry 800x400
Xgraph DataFile.txt DataFile2.txt –bg white –tk –bb –m -M –t “Marker” –geometry 800x400
View more...
Comments