Sunteți pe pagina 1din 64

1 : UNIX

INTRODUCTION
INTRODUCTION TO UNIX

What Is UNIX?

• UNIX is a computer operating system.


• An operating system is the program that controls all the other parts of a computer
system, both the hardware and the software. It allocates the computer's resources
and schedules tasks. It allows you to make use of the facilities provided by the
system. Every computer requires an operating system.
• UNIX is a multi-user, multi-tasking operating system. Multiple users may have
multiple tasks running simultaneously. This is very different than PC operating
systems.
• UNIX is a machine independent operating system. Not specific to just one type of
computer hardware. Designed from the beginning to be independent of the
computer hardware.
• UNIX is a software development environment. Was born in and designed to
function within this type of environment.
• The "UNIX" trademark, previously owned by AT&T and then deeded to UNIX
Systems Laboratories (USL), an AT&T subsidiary, passed to Novell when it
acquired USL. After a brief period of negotiations with rival Unix vendors Sun
Microsystems, Santa Cruz Operation, International Business Machines, and
Hewlett-Packard, Novell granted exclusive licensing rights to the UNIX trademark
to X/Open Co. Ltd., an Open Systems industry standards branding agent based in
the United Kingdom.

Why UNIX?

• Hardware independence
o operating system code is written in C language rather than a specific
assembly language
o operating system software can be easily moved from one hardware system to
another
o UNIX applications can be easily moved to other UNIX machines. Porting is
usually as simple as transfer of the source and a recompile
• Productive environment for software development
o rich set of tools
o versatile command language
• UNIX is available at virtually all HPC centers, allowing researchers relative ease in
utilizing the facilities at each center.
• Distributed processing and multi-tasking

Architecture of UNIX

UNIX Components

• Kernel
o The core of the UNIX system. Loaded at system start up (boot). Memory-
resident control program.
o Manages the entire resources of the system, presenting them to you and
every other user as a coherent system. Provides service to user applications
such as device management, process scheduling, etc.
o Example functions performed by the kernel are:
 managing the machine's memory and allocating it to each process.
 scheduling the work done by the CPU so that the work of each user is
carried out as efficiently as is possible.
 accomplishing the transfer of data from one part of the machine to
another
 interpreting and executing instructions from the shell
 enforcing file access permissions
o You do not need to know anything about the kernel in order to use a UNIX
system. These details are provided for your information only.
• Shell
o Whenever you login to a Unix system you are placed in a shell program. The
shell's prompt is usually visible at the cursor's position on your screen. To
get your work done, you enter commands at this prompt.
o The shell is a command interpreter; it takes each command and passes it to
the operating system kernel to be acted upon. It then displays the results of
this operation on your screen.
o Several shells are usually available on any UNIX system, each with its own
strengths and weaknesses.
o Different users may use different shells. Initially, your system adminstrator
will supply a default shell, which can be overridden or changed. The most
commonly available shells are:
 Bourne shell (sh)
 C shell (csh)
 Korn shell (ksh)
 TC Shell (tcsh)
 Bourne Again Shell (bash)
o Each shell also includes its own programming language. Command files,
called "shell scripts" are used to accomplish a series of tasks.
• Utilities
o UNIX provides several hundred utility programs, often referred to as
commands.
o Accomplish universal functions
 editing
 file maintenance
 printing
 sorting
 programming support
 online info
 etc.
o Modular: single functions can be grouped to perform more complex tasks
Advantages
• Full multitasking with protected memory. Multiple users can run multiple
programs each at the same time without interfering with each other or crashing the
system.
• Very efficient virtual memory, so many programs can run with a modest amount of
physical memory.
• Access controls and security. All users must be authenticated by a valid account
and password to use the system at all. All files are owned by particular accounts. The
owner can decide whether others have read or write access to his files.
• A rich set of small commands and utilities that do specific tasks well -- not
cluttered up with lots of special options. Unix is a well-stocked toolbox, not a giant do-
it-all Swiss Army Knife.
• Ability to string commands and utilities together in unlimited ways to accomplish
more complicated tasks -- not limited to preconfigured combinations or menus, as in
personal computer systems.
• A powerfully unified file system. Everything is a file: data, programs, and all
physical devices. Entire file system appears as a single large tree of nested directories,
regardless of how many different physical devices (disks) are included.
• A lean kernel that does the basics for you but doesn't get in the way when you try
to do the unusual.
• Available on a wide variety of machines - the most truly portable operating system.
• Optimized for program development, and thus for the unusual circumstances that
are the rule in research.

Disadvantages
• The traditional command line shell interface is user hostile -- designed for the
programmer, not the casual user.
• Commands often have cryptic names and give very little response to tell the user
what they are doing. Much use of special keyboard characters - little typos have
unexpected results.
• To use Unix well, you need to understand some of the main design features. Its
power comes from knowing how to make commands and programs interact with each
other, not just from treating each as a fixed black box.
• Richness of utilities (over 400 standard ones) often overwhelms novices.
Documentation is short on examples and tutorials to help you figure out how to use the
many tools provided to accomplish various kinds of tasks.

UNIX Editors

• What is a text editor?


o A text editor is a program which enables you to create and manipulate
character data (text) in a computer file.
o A text editor is not a word processor although some text editors do include
word processing facilities.
o Text editors often require "memorizing" commands in order to perform
editing tasks. The more you use them, the easier it becomes. There is a
"learning curve" in most cases though.
• There are several standard text editors available on most UNIX systems:
o ed - standard line editor
o ex - extended line editor
o vi - a visual editor; full screen; uses ed/ex line-mode commands for global
file editing
o sed - stream editor for batch processing of files
• In addition to these, other local "favorites" may be available:
o emacs - a full screen editor and much more
o pico - an easy "beginner's" editor
o lots of others

The Standard Display Editor - vi

• vi supplies commands for:


o inserting and deleting text
o replacing text
o moving around the file
o finding and substituting strings
o cutting and pasting text
o reading and writing to other files
• vi uses a "buffer"
o While using vi to edit an existing file, you are actually working on a copy of
the file that is held in a temporary buffer in your computer's memory.
o If you invoked vi with a new filename, (or no file name) the contents of the
file only exist in this buffer.
o Saving a file writes the contents of this buffer to a disk file, replacing its
contents. You can write the buffer to a new file or to some other file.
o You can also decide not to write the contents of the buffer, and leave your
original file unchanged.

Three modes of vi:

vi operates in three different "modes":

o Command mode
 vi starts up in this mode
 Whatever you type is interpreted as a command - not text to be
inserted into the file.
 The mode you need to be in if you want to "move around" the file.
o Insert mode
 This is the mode you use to type (insert) text.
 There are several commands that you can use to enter this mode.
 Once in this mode, whatever you type is interpreted as text to be
included in the file. You can not "move around" the file in this mode.
 Must press the ESC (escape) key to exit this mode and return to
command mode.

o Execution mode
 Vi ends up in this mode
 Whatever command you give is executed and the result is
shown on the screen.

How to create a vi file?


To create a file such as factorial on a UNIX based computer, some familiarity with vi is
necessary. Type vi factorial. You are now in command mode. Typing a allows one to
enter text entry mode - any characters subsequently typed at the keyboard will be entered
after the cursor. To finish text entry, hit the escape key. Then type ZZ or :wq to “write”
the file.

Here is a short summary of vi commands:

• Entering vi

vi filename - The filename can be the name of an


existing file or the name of the file
you want to create.
view filename - Starts vi in "read only" mode. Allows
you to look at a file without the risk
of altering its contents.

• Exiting vi

:q - quit - if you have made any changes, vi


will warn you of this, and you'll need
to use one of the other quits.
:w - write edit buffer to disk
:w filename - write edit buffer to disk as filename
:wq - write edit buffer to disk and quit
ZZ - write edit buffer to disk and quit
:q! - quit without writing edit buffer to disk

• Positioning within text

By character
left arrow - left one character
right arrow - right one character
backspace - left one character
space - right one character
h - left one character
l - right one character

By word
w - beginning of next word
nw - beginning of nth next word
b - back to previous word
nb - back to nth previous word
e - end of next word
ne - end of nth next word

By line
down arrow - down one line
up arrow - up one line
j - down one line
k - up one line
+ - beginning of next line down
- - beginning of previous line up
0 - first column of current line (zero)
^ - first character of current line
$ - last character of current line

By screen
CTRL-f - forward 1 screen
CTRL-b - backward 1 screen
H - top line on screen
M - mid-screen
L - last line on screen

Within file
nG - line n within file
1G - first line in file
G - last line in file

• Inserting text

a - append text after cursor


A - append text at end of line
i - insert text before cursor
I - insert text at beginning of line
o - open a blank line after the current
line for text input
O - open a blank line before the current
line for text input

* Note: hit ESC (escape) key when finished inserting!

• Deleting text

x - delete character at cursor


dh - delete character before cursor
nx - delete n characters at cursor
dw - delete next word
db - delete previous word
dnw - delete n words from cursor
dnb - delete n words before cursor
d0 - delete to beginning of line
d$ - delete to end of line
D - delete to end of line
dd - delete current line
ndd - delete n lines (start at current line)
“xdd - deletes current line and copies it to a buffer named x. (x
can be any alphabet)
“xndd - deletes n lines and copies it to a buffer named x.
“xdw - deletes current word and copies it to a buffer named x.
“xndw - deletes n words from the current cursor position and copies
it to a buffer named x.

• Changing text

r - overtype only 1 character


R - overtype text until ESC is hit
J - join two lines

* Note: hit ESC (escape) key when finished changing!

• Copying lines

yy - "yank": copy 1 line into buffer


nyy - "yank": copy n lines into buffer
yw - “yank”: copies word into buffer
y$ - “yank”: copies line from cursor position to the end of
line
y0 - “yank” : copies line from the cursor position to the
beginning of the line.
1p - pastes last yanked buffer
2p - pastes last but one yanked buffer

“xyy - yanks current line to a buffer named x.(x can be any


alphabet)
“xnyy - yanks n lines from current line to a buffer named x.
“xyw - yanks current word to a buffer named x.
“xnyw - yanks current word from current cursor position to a buffer
named x.
“xp - pastes content of buffer x at current cursor position.

• Moving lines (cutting and pasting)

ndd - delete n lines (placed in buffer)


p - put contents of buffer after current
line
P - put contents of buffer before current
line

• Miscellaneous commands

u - undo the last command (including undo)


. - repeat last command
xp - swap two adjacent characters

:r filename - read/insert contents of filename after


current line.

• Block commands

:nd - deletes nth line.


:m,nd - deletes lines from m to n
:n mo p - moves line m after line p
:m,n mo p - moves line m to n after p
:m co p - copies line m after line p
:m,n co p - copies line m to n after line p
:m,n w filename- writes line m to n to a file
:m,n w>>filename- appends line m to n to a file
• Abbreviation

:abbr <abbreviation> <longform> - an abbreviation is defined for a


Longform. When we type the
Abbreviation followed by a space,
It is replaced by its longform
:abbr - lists currently defined
abbreviation
:una abbr - unabbreviates an abbreviation

How to close a vi file?

To save any changes to your file and then exit the vi editor, press the Escape key and type
:wq (for write and quit), then press Enter or Return to execute the command. You may
also save any changes and exit vi by pressing the Escape key and then entering the
command ZZ (note the capitals). As you're working, you might also decide that you do
not want to save your changes, and that you wish to exit vi and leave your file as it was
when you started. To do this, press the Escape key to enter command mode, then type :q!
or :quit! and press Enter or Return.

How to delete a vi file?

To delete a vi file such as factorial on a UNIX based computer, you just have to type rm
factorial. You can delete the vi file when you are on the terminal screen.
2 : UNIX SHELL
PROGRAMMING
PROGRAM I : Program to perform a simple shell
programming.

THEORY: A worthwhile program needs to talk to the user, and in turn requires a
language. Two very basic words in the shell vocabulary are read, to accept input by the
user, and echo, to display output to the user.

Suppose a name, entered, is read by the shell, and name is a variable wherein it is
collected. Now, if we use, $name, in the echo statement, then the value of the variable
name will be extracted and will be echoed on the screen.

read name
echo $name

We as Owners have the permission to read and write to the file, but not to execute it. The
remedy to this is the chmod command.

$chmod 744 filename

This command changes the mode or the permission available with a file. The new
permissions for file are therefore 744.
PROGRAM II: Arithmetic expressions in shell
programming.
THEORY: To carry out any arithmetic operations on shell variables we have to use
the command expr which is capable of evaluating an arithmetic expression.
There are several things that should be noted in this program:

• Comment about the program should be preceded with a #. Moreover, any number
of comments can be given at any place in the program.
• More than one assignment can be done in a single statement (line).
• In addition to normal addition (+), subtraction (-), multiplication (*) and division
(/) the expr command can also perform a modular (%) division operation. It
returns the remainder obtained on dividing the first number by the second. The
remainder always take the sign of the numerator.
• The multiplication symbol must always be preceded by a \. Otherwise the shell
treats it as a wildcard character for all files in the current directory.
• Terms of the expression provided to expr must be separated by blanks. For
example, the expression 10+20 is invalid.
• While evaluating an expression expr performs the various arithmetic operations
according to the following priorities.
/ , * , % - First priority
+,- - Second priority
• expr is capable of carrying out only integer arithmetic.
Program to implement if – then –
PROGRAM III:
else statement and to show the use of logical
operators.
THEORY: The if statement by itself will execute a single command, or a group of
commands, when the exit status of the control command is 0. It does nothing when the
exit status is 1. We can execute one group of commands if the exit status is 0 and
another group if the exit status is 1. This is what is the purpose of the else statement.
The working of if depends upon the exit status of the control command. Therefore, the
sort of tests are performed and result is translated into the language of success or
failure. This helps the shell in deciding whether to execute the commands in the if
block or the commands in the else block.
Various tests which can be performed on a block of statements are:
• Numerical tests
• String tests
• File tests
Numerical tests are used when comparisons b/w values of 2 nos. is to be done.

OPERATOR MEANING
-gt greater than
-lt less than
-ge greater than or equal to
-le less than or equal to
-ne not equal to
-eq equal to
Shell allows usage of three logical operators while performing a test. These are:
1. -a (Logical AND)
2. -o (Logical OR)
3. ! (Logical NOT)
PROGRAM IV: Program to print fibonacci series i.e. 0
1 1 2 3 5... using for loop.

THEORY: The for loop is most frequently used as compared to the while and the
until loops. Its working is also different than the other two loops. The for allows us to
specify a list of values which the control variable in the loop can take. The loop is then
executed for each value mentioned in the list.
The general form of for statement is as under:

for control-variable in value1 value2 value3 …


do
command1
command2
command3
:
:
:
done

Here the for loop would execute the same sequence of commands for values mentioned
in the list following the keyword in.

The values mentioned after in are values, the control-variable takes in succession as the
shell executes the for loop. That is, firstly the instructions between do and done are
executed with control-variable set to a value ‘value1’. Then control-variable is set to next
value ‘value2’, and the instructions between do and done are executed again. All the
commands between do and done are executed for each value of control-variable. This
continues until all the values in the list are used.
PROGRAM V: Program to implement case statement.
THEORY: Though if-else constructs can be nested, an abundance of conditions may
make tracing the control in a program difficult. Hence, for a program where we are
required to select from several alternatives a case control structure is beneficial.
For example:

case value in
choice1)
do this
and this
;;
choice2)
do this
and this
;;
:
:
:
*)
do this
;;
esac

The “do this” and “and this” lines in the above form represent any valid Unix command.
Also, choice1 and choice2 are labels which identify the potential choices of action.
Firstly, the expression following the case keyword is evaluated. The value that it yields is
then matched, one by one against the potential choices. When a match is found, the shell
executes all commands in that case upto ;;. This pair of semicolons, placed at the end of
each choice, are necesarry. On encountering semicolons the control is transferred to esac,
the keyword denoting the end of a case. The last case ‘*)’ represents the default clause of
the case control instruction. It gets executed when all other cases fail.

‘$#’ parameter is used to find out the number of positional parameters which were set.
‘$*’ stands for all command line arguments following the name of the script.
PROGRAM VI: Program to find the factorial of a
number using while loop.

THEORY: A loop involves repeating some portion of the program either a specified
number of times or until a particular condition is being satisfied.

 In a program, the while loop allows us to do something a fixed number of times. The
general form of while is as shown below:

while control-command
do
command 1
command 2
:
:
:
done

 The statements within the while loop would keep on getting executed till the exit
status of the control command remains true (0). When the exit status of the control
command turns out to be false (1), the control passes to the first command that
follows the body of the while loop (i.e. the first command after done).

 The statements within the loop may be a single command or a group of commands.
In either case it is necessary to put them between do and done.

 As a rule the while must have a control command that will eventually return an exit
status 1 (false), otherwise the loop would be executed forever, indefinitely. The value
of the loop counter remains the same forever. So to get the body of the loop executed
repeatedly, we increment or decrement the loop counter.
3 : DATA
MANIPULATION
WITH AWK
PROGRAM I: Program to implement searching of
strings in a file i.e. to
implement pattern matching.

THEORY: Like any text editor, vi too is equipped with a mechanism to search strings
in a file. These strings are more correctly known as patterns. The patterns can be searched
from beginning to end and from end to beginning. A lot of variations can be made in
building the pattern to be searched. For example: if we want to search a record named
fish then we can use pattern /fish/.

The awk is a pattern-matching tool, but with the added ability to perform specified, often
complex, operations on records or on specific fields in records after a pattern is matched.
Now the following commands used for searching the patterns along with their results
shown in the following screen shot:
1. awk ‘/Meat/ {print}’ gdbase or awk ‘/Meat/’ gdbase

This command displays all lines in file gdbase containing the pattern (string of
characters) Meat. The default action of awk is to print the whole of any record that meets
the selection criteria. Inside slashes, the string is a regular character string for matching.

2. awk ‘{print $0}’ gdbase or awk ‘{print}’ gdbase

This command displays every record of the entire file gdbase. The predefined variable $0
has a value of the whole record – i.e., the entire line.

3. awk ‘{print $1}’ pattern


This command displays the first field of all records in file gdbase.
4. awk ‘{print $3 $1 $2}’ gdbase

This command displays the third field, followed by the first field, followed by the second
field of all records in file gdbase, without spaces between the fields. There are no spaces
separating the fields in the output, because we included no instruction telling awk to put
spaces between the fields.

5. awk ‘{print $3, $1, $2}’ gdbase

This command displays the third field, followed by the first field, followed by the second
field of all records in file gdbase, with a space between fields. The commas separating
the fields instruct awk to insert spaces after the third and the first field in the output.
6. awk –v item=’Hshld’ ‘{print item,$1}’ gdbase

This command displays the value of the variable item i.e. Hshld followed by a space,
then the value of the first field. -v assigns the value Hshld to the variable item before
execution of the program begins. Quotes instruct shell to pass as it is. ‘=’ is the
assignment operator. It assigns the value on right to variable to left.

7. awk ‘{print item, $1}’ gdbase

This command displays the value of the first field with a space at the beginning of each
record. We did not define the variable; awk interprets the string item as a variable, cannot
find it, and ignores it. The comma is still interpreted.
8. awk ‘{print “Item:”, $1, “Price:”,$3}’ gdbase

This command displays the literal string of characters Item:, followed by a space, then
the value in field 1, then a space, followed by the string Price:, then another space, and
the value in field 3. Inside double quotes a string is just a string; the have no special
meaning.

9. awk ‘{print 12345, $1}’ gdbase

This command displays the number 12345 followed by the value of the variable $1, the
contents of field 1. Numbers are not interpreted as variables and are not quoted.
10. awk ‘/[Vv]eg/ {print $0}’ gdbase

This command displays all the lines containing either Veg or veg in any field. The pattern
/[Vv]eg/ matches either V or v followed by eg.

11. awk ‘$5 == “y” {print $0}’ gdbase

This command displays all the records containing exactly one character, y, in the fifth
field. There are many lines with a y in other fields that are not chosen. ‘==’ is the
relational operator. It evaluates to true if left side value matches right side value.
12. awk ‘/Dairy/||/Meat/ {print}’ gdbase

This command displays all the lines containing either of the string—Dairy or Meat,
anywhere on the line. The pattern /Dairy/||/Meat/ is considered matched when a line has a
match of either string. ‘||’ is the logical operator. It evaluates to true if either of the
condition, right or left, is true. Whereas, ‘&&’ operator evaluates to true if both the
conditions are true.

13. awk ‘$3 < 1.99’ gdbase

This command displays all records having a third field numeric value less than 1.99. ‘<’
is the relational operator. It evaluates to true if right side value is less than left side value.
Whereas, ‘>’ operator evaluates to true if right side value is larger than left side value.

14. awk ‘$3 ~ /\.39/’ gdbase

This command displays all records containing string .39 anywhere in the third field. ‘~’ is
the relational operator. It tells awk to search the third field of records and select the
record if the string .39 is present in the field. The backslash is included to indicate that
the period is just a period—not a regular expression special metacharacter.

15. awk ‘$2 ~ /ry$/’ gdbase

This command displays all records containing an ry at the end of field 2. The $ is the
“end of a line or field” metacharacter.
PROGRAM II: Program to examine awk command
files, perform
arithmetic operations, format
output, and explore use of
other related patterns (BEGIN and
END).

THEORY: awk incorporates a wide range of data matching, modifying, and


programming features. In addition to pattern-matching, awk is fully programmable—
capable of supporting the loops, conditional statements, and variables expected in a
programming language.
1. FIRST COMMAND
Create a command file named print.dairy containing the following awk statement:

Command files such as print.dairy are not executable files, but merely text files that are
specially written for and read by awk.
The resulting output is the first and third fields of all records in gdbase that contain the
string Dairy. To awk, the -f is the instruction to open the file named as the next
argument, i.e. print.dairy, for instructions to be applied on the data of file gdbase.

2. SECOND COMMAND
Create a new command file named findNR containing the following awk statement:

The resulting output is the sixth record (line) of the file gdbase. The output is determined
by the contents of the command file findNR that is just created. The element NR is
another predefined awk variable, denoting Number of the Record.

3. THIRD COMMAND
Create a new command file named findNF containing the following awk statement:
The negation operator (!) reverses the sense of the selection. With the !, the lines with 6
fields are rejected. All lines are displayed when we ask for lines that have other than six
fields. The element NF is another predefined awk variable, denoting Number of the
Fields and is set to the number of fields in the current line.

4. FOURTH COMMAND
Create a file named great-tchrs, consisting of grade of school teachers.
Create an awk command file named awk-cmdA consisting of the following lines:

The output is not one record to a line, but many records to a line separated by a series of
dashes. Output Field Separator (OFS) determines the output separator for Fields, and
Output Record Separator (ORS) controls the output separator for Records. The value of
OFS is a space by default and the value of ORS is a newline by default. And, the input
fields are separated with colons “:”

5. FIFTH COMMAND
Create a new file named manyrecords with the following content:
Create another awk command file called awk-cmdB containing:

The value of FS is the value of the Field Separator; the default separators (delimiters) are
one or more spaces, or a tab. The value of RS is the value of the Record Separator; the
default separator is a newline character. The fields at input are separated by : and records
are separated by the $ character. The fields at output are separated by spaces and records
by new lines, as defaults.

6. SIXTH COMMAND
Copy the file print.dairy to a new file named print.dairy2 using the following cp
command. print.dairy2 will have same content as it was in print.dairy.

7. SEVENTH COMMAND
Modify the new file by deleting the old action and inserting three new lines, as follows:
The symbol ‘=’ is the assignment operator. It stores the value of an expression in a
variable. Here, the variable name is assigned the value of the first field, $1. The variable
price is assigned the value of the third field, $3. The print statement displays the
variable’s values, not the literals name and price.

8. EIGHTH COMMAND : awk ‘{item = $1; print item, item}’ gdbase


After the variable item is assigned the first field value of gdbase for each record, the
resulting value in each record is printed twice per line i.e. the value of item as a variable
is printed twice.

9. NINTH COMMAND : awk ‘{print $1, $2, $3 - .10, $4, $5}’ gdbase
The number .10 is not enclosed in quotes. Numerical arguments are considered by awk to
be variables having values equal to their inherent numeric values. The subtraction
operator ‘-‘ is the arithmetic operator. Here, this operator instructs awk to subtract a
constant from the value of a field in each record output by the print statement. All the
five fields of gdbase are displayed, but with its original third field values reduced by .10.
10.TENTH COMMAND
Create a new command file called change containing the following program:
In this, each record’s original price is divided by the value of the change variable 3 and
the result assigned to the variable saleprice. Further each record’s third field, price, has
its value reduced by the constant .10 before printing. The print line outputs the value of
the new variable saleprice as the fourth field.

11.ELEVENTH COMMAND
Copy change to a file called running and modify it to contain the following lines:
The ‘+=’ is the assignment operator. It takes the value held by the variable to the left of
the operator, adds to it the value specified on the right of the operator, and assigns the
result of the sum back to the variable on the left of the operator. In awk, a new variable
has a value of zero at the time of its creation. Here, a new variable running is created, and
is assigned the sum of zero plus the current value of total. When the next record is
processed, the variable running is assigned the sum of its present nonzero value plus the
value of total. This process continues until all records in the file are processed. The value
of running increases as each total is added. “running += total” can also be written as
“running = running + total”.

The output includes one line for each item, showing the item’s name, total cost (price
multiplied by quantity purchased), and the current running total. The operate-and-assign
operators (+=, -=, *= and /=) are also supported in awk. A variable to the left is operated
on (+, -, *, /) by an element to the right. The result is then assigned back to the variable to
the left.
12.TWELFTH COMMAND
Create a new command file called taxes that contains the following lines:

Here printf is the formatting statement. The % is a placeholder for a value. The s
specifies the type of value to be inserted; here, a string of characters. The \n at the end of
the control string tells printf to print a new line at that position. Here, first specifier %s
refers to the value in variable $1, and second specifier %s refers to the value in variable
taxedprice.

This application selects all records having y in the fifth field, indicating that the
associated item is taxable. The taxes program then calculates a taxedprice equal to price
plus 6.5 percent of price. printf if commonly used in awk code to provide additional
formatting capabilities over basic print.
13.THIRTEENTH COMMAND
Reopen the file taxes. Modify the printf statement as follows:

The format specifiers -20 and 10 have altered the appearance of the output. Here, the
specifier %-20s refers to the value in variable $1, and the specifier %10s applies to
taxedprice.

Ouput for this command is same as that of the previous command, but with a slight
alteration in the appearance of the output. %-20s prints the value held by $1, left-justified
to 20 number of spaces and %10s prints the value held by taxedprice, right-justified to
10 number of spaces.

14.FOURTEENTH COMMAND
Reopen the file taxes. Modify the printf statement as follows:
%10.2f prints the value of taxedprice as a floating-point number, rounded to the 2nd
decimal point, right-justified to the 2nd space. Rest of the output and formatting is same as
that of the previous command.

This modification instructed printf to express the variable taxedprice as a floating-point


number held to a precision of two decimal places rather than a string. This result in an
improved alignment.

15.FIFTEENTH COMMAND
Reopen the file running. Modify it as follows:
The BEGIN statement instructs awk to execute the associated instructions before
processing the database. The END statement instructs awk to execute the associated
instructions after processing the database. The words BEGIN and END, must be
followed by an opening brace { in the same line.

Before entering the main loop, the awk prints the quoted phrase “The running totals
are:”, i.e. BEGIN statement is executed. This application of running is identical to the
previous running application except that the running total is not printed as each line is
processed. Instead, it is printed after all records are processed. After all lines are
processed, the END statement is executed, and its associated action—printing the
running total—is performed.
4 : UNIX
PIPING
PROGRAM: Program to construct pipelines to carry
out the various
jobs.

THEORY: The Unix piping facility lets us connect commands and operations to other
commands and operations. It can be really useful to redirect the out of one program so
that it becomes the input of another program, thereby joining the two programs. To send
the output of one command as input for another, the two commands must be joined using
a pipe (|) character.

A filter is a program which can receive a flow of data from standard input, process (or
filter) it, and send the results to the standard output.

FILTER PURPOSE

cat Concatenates and displays files


more Displays a file one screenful at a time
head Prints the first few lines of a file
tail Displays the last few lines o a file
grep Searches files for a pattern
sort Sorts and merges files
nl Adds line numbers to a file
wc Counts lines, words and characters
tee Creates a tee in a pipe
uniq Reports repeated lines in a file
tr Translates characters in a file
cut Cuts out selected fields of each line of a file
paste Merges lines of files
lpr Sends requests to line printer
Create a file named testfile containing following lines:
1. FIRST COMMAND

The output of testfile is displayed on the screen with the value of total number of users
who have logged in, displayed at the bottom of the list. ‘-l’ used with wc shows only the
number of lines. wc used with ‘-c’ and ‘-w’ shows the number of characters and words,
respectively in the file.

2. SECOND COMMAND

The output of testfile is displayed on the screen and the lines containing the word ‘root’
are counted and the count is displayed at the end of the list. Further, this count is stored in
a file named countfile1 using ‘>’ operator.
3. THIRD COMMAND
The output of files testfile and gdbase are displayed together. Further, this output is
appended to the file newfile. Contents of newfile, after appending, are shown above.

4. FOURTH COMMAND

From the output of testfile, the lines containing word ‘root’ are displayed on the screen
along with the count displayed at the end of the list. Here, the lines containing word
‘root’ are first stored in a file named file1 then the lines, words and characters are counted
of the file file1.

5. FIFTH COMMAND
This output has two files combined together. The list of file testfile is combined with the
list in the file gdbase. This combined list is further sorted and the sorted output is stored
in a new file named newfile1. Then the contents of newfile1 are displayed using more
command.

6. SIXTH COMMAND

From the file testfile, the lines containing word ‘root’ are displayed in a sorted order.
Further, all the lines are numbered using nl command.

7. SEVENTH COMMAND

In this output, all the lines of testfile and gdbase are merged together. The testfile lines
are displayed followed by the gdbase lines. Only, 10 lines of gdbase are merged with
testfile because testfile itself has 10 lines. Rest of the 3 lines of gdbase are displayed with
a tab space at the end of the merged list.

8. EIGHTH COMMAND

Firstly, all the lines of testfile are merged with lines of gdbase. Secondly, merged list is
sorted. In the output, only the first few lines of the merged list are displayed due to head
command.

9. NINTH COMMAND
Firstly, all the lines of testfile are merged with lines of gdbase. Secondly, merged list is
sorted. In the output, only the last few lines of the merged list are displayed due to tail
command.

10.TENTH COMMAND

-d delim : The character following -d is the field delimiter (-f option only). Default is tab.
Space or other characters with special meaning to the shell must be quoted. delim can be
a multi-byte character. Here we used a space as a delimiter in single quotes.
-f list : The list following -f is a list of fields assumed to be separated in the file by a
delimiter character (-d ). Here –f 1,3-4 cuts the first, third and fourth fields of file testfile
and displays it on the screen.

S-ar putea să vă placă și