Вы находитесь на странице: 1из 73

TCL Programming

Dr. K. Ganesan
Director – TIFAC-CORE in Automotive
Infotronics &
Senior Professor,
School of Information Technology and
Engineering
VIT University
Tcl Programming
• Tcl (Tool Command Language) is used by millions of
people in the world.
• It is a language with a very simple syntax and it allows a
very easy integration with other languages.
• Tcl was created by Jhon Ousterhout, and the
characteristic of this language are:
• It allows fast development
• It is compatible with many platforms
• It is flexible for integration
• It is easy to use
• It is free
Commands evaluation
• Each Tcl command call is a sentence of the form
:
• command arg1 arg2 arg3 ...
• The Tcl evaluator take each word of this
sentence and evaluates it.
• After evaluation of each word, the first word
(command) is considered to be a function name
and this function is executed with arguments as
the following words.
• To evaluate a word, the interpreter has to do 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<=1024} {set x [expr {$x * 2}]} {
• puts "x is $x"}
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}]"
• }
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
• }
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:
• exec xgraph data
Defining a list
• A list in Tcl consists of a string whose items are separated by white
space. For example, this is a list,
• ABCDE
• 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]
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
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.
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
output
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 class
inherits from another one, in the below example the kid
class inherits from mom class.
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?”
• }
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?”
Awk Programming

Dr. K. Ganesan
Director – TIFAC-CORE in Automotive
Infotronics &
Senior Professor, School of Information
Technology and Engineering
VIT University, Vellore – 632 014
Introduction
• Awk utility is powerful data manipulation
/scripting programming language (In fact based
on the C programming Language).
• One can use awk to handle complex task such
as calculation, database handling, report
creation etc.
• Consider following text database file - inven.tr
• 1. Pen 5 20.00
• 2. Rubber 10 2.00
• 3. Pencil 3 3.50
• 4. Cock 2 45.50
Introduction
• In above file, fields are Sr.No, Product, Qty, Unit Price.
• Field is the smallest element of any record.
• Each field has its own attributes.
• For e.g. Take Qty field. Qty. field’s attribute is its
numerical value.
• Collection of fields is known as record.
• So
1. Pen 5 20.00 ----> Is a Record.
• Collection of records is known as database file.
• In above text database file each field is separated using
space (or tab character) and record is separated using
new-line character (i.e. each record is finished at the end
of a line ).
• In awk, fields are accessed using a special variable.
• For e.g. In above database $1, $2, $3, $4 respectively
represents Sr.No, Product, Qty, Unit Price fields
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.
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
15
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<=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).
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.
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):
• 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 <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.
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.
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)
• Width, in pixels, of normal grid lines.
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.
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.
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 <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)
• 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
Xgraph DataFile.txt –fmtx A –fmty
B –t “Format” –geometry 800x400

Вам также может понравиться