Unit Part II
Unit Part II
&
AWK PROGRAMMING
1
Shell
2
Functions of Shell
2. Program Initialization
3. Input-Output Redirection
4. Pipeline Connection
5. Substitution of Filename
6. Maintenance of variable
7.Environment Variable
8. Shell programming
3
Types of Shell
Bourne Shell
Korn Shell
C Shell
4
Escape Sequence
Option Action
\b Back Space
\n New line
\r Carriage Return
\c Positioning the Cursor
\t Tab
\a Alert (Beep Sound)
\\ Back Slash
\‟ Single Quote
\‟‟ Double Quote
5
Example
6
Escape Sequence
Option Action
7
Example
$echo “\033[1m This is Bold”
8
The tput command
9
Option Action
clear Clear the screen
cup r c Move cursor to row r and column c
bold Bold display
blink Blinking display
rev Reverse video display
smso Start mode bold
rmso End mode bold
smul Start mode underline
rmul End mode underline
bel echo the terminal‟s “bell” character
lines echo number of lines on the screen
cols echo number of columns on the screen
ed Clear to end of the display
el Clear to end of line
10
#Demonstration of tput command
tput bell
tput clear
echo “ Total no. of column on screen =\c”
tput cols
echo “Total no. of row on screen = \c”
tput lines
echo “This is a normal message”
tput blink
echo “This is a blinking message”
tput bold
echo “This is a bold message”
tput cup 10 20 # Positions cursor at 10th row and 20th column
echo “Testing of tput”
tput smso # start bold mode
Echo “ The bold has begun………..”
Tput rmso # end bold mode
11
Command Grouping
Syntax:-
command1;command2;command3
For Example :-
1. date;pwd;ls
II. (date;pwd;ls)>result
12
set
$set friends come and go but
enemies accumulate
$echo $1 $2 $3 $4 $5 $6 $7
O/P:- friends come and go but
enemies accumulate
$echo $1 $2 $3 $4
O/P:- friends come and go
13
Positional Parameters
$0 – Refers to the name of the command.
$1 – Refers to the first argument.
$2 – Refers to the second argument. … & so on
$* - Refers all the argument.
$# - Refers the total no. of arguments.
$? - Returns the exit status of last executed
command.
$! - Returns the Process Identification number (PID)
of last background command (command ended
with & )
$$ - Returns the PID of the current shell.
14
$set Do you want credit or results
$set A smiling face is always beautiful
$echo $1 $2 $3 $4 $5 $6
O/P:- A smiling face is always beautiful
$cat lucky
O/P:- Give luck a little time
$set `cat lucky`
$set $1 $2 $3 $4 $5
O/P:-Give luck a little time
15
Using Shift on Positional Parameters
O/P:-
You have the capacity to learn from mistakes.You
You0 You1
16
shift
$shift 7
$echo $1 $2 $3 $4 $5 $6 $7 $8 $9
mistakes.You will learn a lot in your life.
OR
$echo $*
18
Write a shell program to perform the
positional parameters and shift command
#Program of positional parameters and shift command
set If you are headed in the wrong direction, God allows U
turns.
echo $*
shift 1
echo $*
shift 1
echo $*
shift 1
echo $*
Shift
echo $*
19
Operators
Arithmetic Operator:
+,-,\*,/ and %
Relation Operator:
-lt, -le, -gt, -ge, eq, -ne
Logical Operator:
-a( AND), -o (OR), !(NOT)
20
String Operators
21
Testing for files
22
Taking Decision
The if-then-fi statement
The if-then-else-fi statement
The if-then-elif-else-if statement
The case-esac statement
23
if–then–fi statement
if [ <condition> ]
then
statement 1….statement n
fi
eg- echo “Enter source and destination file names
24
if–then–else-fi statement
if <condition>
then
statement 1
else
statement 2
fi
eg- echo “Enter source and destination file names
read source destination
if [ cp $source $destination ]
then
echo “ File Copied Successfully”
else
echo “Failed to copy the file
fi
25
if–then–elif–else-fi statement
if <condition>
then
statement 1
elif <condition>
statement 2
else
statement 3
fi
26
case-esac statement
case <value> in #Example of case statement
choice1)
do this echo “Enter the number from 1 to 3 : “
and this
read num
;;
choice2)
do this case $num in
and this 1) echo You Entered 1
;; ;;
*) 2) echo You Entered 2
;;
do this
3) echo You Entered 3
and this ;;
;; *) echo I said 1 to 3
esac ;;
esac
27
Looping Statement
Using a while statement
Using a until statement
Using a for statement
28
while statement
while [<condition>] count=1
do while[ $count –le 10 ]
statement1
do
statement2
done echo $count
count=`expr $count + 1`
done
29
count=1
until statement
while[ $count –lt 10 ]
do
until [<condition>] echo $count
do count=`expr $count + 1`
statement1 done
statement2
count=1
done
until[ $count –gt 10 ]
do
echo $count
count=`expr $count + 1`
done 30
for statement
for control_variable in value1 value2…. do
statement1
statement2
done
31
AWK Programming
The command grep lets you locate words or patterns in a file. Next
sed lets you add or delete lines depending upon a pattern. You
could replace the words or change the case and so forth. Now we
come to the granddaddy of utilities i.e. awk.
awk stands for Aho , Weinberger & Kernighan the creators of
this utility.
awk can do pattern matching, print selected records/fields and
comparison & computation.
We will take a look at how awk operates. awk operates like sed i.e.
It processes the input line by line. Also it has to have a context or
reference. The similarity ends here. When it comes to actions to be
performed on the selected lines awk provides you a lot more
features.
32
awk operations
awk‟s rules of the thumb or Simple awk Filters
Column selection or Splitting line into fields
Extended Regular expression [ERE]
Numerical Operators
The –F & f options
Files
User Defined Variables
Formatting Output
Redirection
BEGING and END Patterns
33
awk‟ rules of the thumb or Simple awk filter
Syntax:-
$awk [option] „selection criteria { print }‟
<filename>
Eg:-
1. $awk „/sales/‟ file1
2. $awk „/sales/ { print }‟ file1
3. $awk „/sales/ { print $0}‟ file1
34
Column Selection or Splitting line into fields
Eg:-
1. $awk „/sales/ { print $1 $2 }‟ file1
2. $awk „/sales/ { print $1,$2 }‟ file1
3. $awk „/sales/ { print $2,$1 }‟ file1
4. $awk „/sales/ { print $3,$1,$2 }‟ file1
35
Extended Regular Expression [ERE]
Eg:-
1. $awk „/[a-m]/ { print }‟ file1
2. $awk „/[a-m].*/ { print $1,$2 }‟ file1
3. $awk „/M[CB]A/ { print $0 }‟ file1
4. $awk „/M[!CB]A/ { print $3,$1,$2 }‟ file1
36
Numerical Operations
1. Assignment Operator :- =
2. Arithmetic Operators :- + , - , * , / , %
3. Logical Operator :- || , && , !
4. Comparison Operators :- < ,> ,<= ,>=
,== ,!=
5. String Comparison:- =~ , !~
37
Examples
38
The –F option
39
File2
Files 101 Ram sales good
102 Shyam Sales Good
$cat>rep.awk 103 John Purchase Good
/sales/ { print $1,$3} 104 Hari Marketing Good
^d
$cat rep
O/P:- /sales/ { print $1,$2,$3}
40
$cat>rep.awk
/sales/ {
name = $2
dept = $3
print “Myself” name “and department is” dept
}
^d File2
$awk –f rep.awk file2 101 Ram sales good
102 Shyam Sales Good
O/P:- 103 John Purchase Good
Myself Ram and department is sales
104 Hari Marketing Good
Myself Shyam and department is sales
41
User Defined Variables
Sundry
Pen 110
$cat>Hike.awk
Pencil 100
{
Marker 200
name = $1
Duster 150
price = $2
inc = 10
new_price = price + inc
print name , price , newprice
}^d Pen 110 120
$awk –f Hike.awk Sundry Pencil 100 110
Marker 200 210
Duster 150 160
42
Formatting Output
File Name :- Extra.awk
/pen/ {
name = $1
price = $2
nprice = price + price * 0.325
printf “%20s %10s \n”, name,nprice
}
$awk –f Extra.awk Sundry
43
Redirection
$awk „/sales/ { print }‟ file1> Nfile1
$cat Nfile1
44
BEGIN and END patterns
45
BEGIN {
print “THIS IS TRIAL”
print “NAME OF OPRICE NPRICE”
}
/pen/ {
name = $1
price = $2
oprice = price
nprice = oprice * 1.25 Sundry
} Pen 110
END { Pencil 100
print name oprice nprice
Marker 200
}
Duster 150
46
Built in variables
VARIABLE FUNCTION
Cumulative number of lines read
NR
FS Input field separator
48
ARRAY
Awk handles one-dimensional arrays.
The index for an array can be virtually
anything; It can even be a string.
No array declarations are necessary;
An array is considered to be declared the
moment it is used and is automatically
initialized to zero, unless initialized
explicitly.
49