Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Awk with Example

Awk1 file

RNO NAMEADDRESS
1 gita adajan
2 shreya gopipura
3 tina goddod road
4 priya adajan
5 smit gopipura

Awk2 file

1|kush|12000|manager
2|jenil|10000|manager
3|priya|30000|programmer
4|nirva|15000|programmer
5|jiya|4000|clerk
6|tiya|6000|clerk

1) awk ‘/gopipura/{print}’ awk1


2) awk ‘/gopipura /{printf “%s”,$3}’ awk1
3) awk -F”|” ‘{print $3,$4}’ awk2 (whenever field is separated by other symbol use -F option)
4) awk -F “|” -f awk1.awk awk2 (storer awk instruction in file )

awk1.awk file

BEGIN{print "begin"}
{
if ($4=="programmer")
{
print $0;
}
}
END{print "end"}

5) to read line : using getline function and create user defined function

function1.awk

BEGIN {
print "Enter the number : "
getline n<"/dev/tty"
display(n);
}
function display(n)
{
print n;
}

output
5 (take from user)
5 (diplay data from user defined function)

matrix.awk

BEGIN {
print "Enter the number : "
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
getline n<"/dev/tty";
a[i","j]=n
}
}
print "Matrix";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf"%3d",a[i","j]
}
printf"\n"
}
}

output

1 2 3
4 5 6
7 8 9

Matrix Addition

BEGIN {
print "Enter Element of 1st Matrix: "
print "Enter the number : "
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
getline n1<"/dev/tty";
a[i","j]=n1
}
}
print "Enter Element of 2nd Matrix: "
print "Enter the number : "
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
getline n2<"/dev/tty";
b[i","j]=n2
}
}
print "Matrix 1: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf"%3d",a[i","j]
}
printf"\n"
}
print "Matrix 2: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf"%3d",b[i","j]
}
printf"\n"
}
print "Matrix Addition: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i","j]=a[i","j]+ b[i","j]
printf"%3d",c[i","j]
}
printf"\n"
}

output
While loop

File name: While1.awk

BEGIN{
i=1
while(i<=10)
{
print i
i++
}
}

Run :

awk -f while1.awk

output :

1
2
3
4
5
6
7
8
9
10

Do while loop

File name: While1.awk

BEGIN{
i=1
do
{
print i
i++
}while(i<=10);
}

Run :

awk -f dowhile1.awk

output :

1
2
3
4
5
6
7
8
9
10

For loop

File name: for1.awk

BEGIN{
for(i=1;i<=10;i++)
print i
}

Run :

awk -f for1.awk

output :

1
2
3
4
5
6
7
8
9
10

String Function

File name: strfun1.awk

BEGIN{
i="TYBCA SEM 5"
print "String : "i
print "String length :"length(i)
print "Return specified string :"substr(i,3,5)
print "Return the specified string position :"index(i,"E")
print "Divided String into array :"split(i,arr," ")
print arr[1]
print arr[2]
print arr[3]
print "Convert into lower case :"tolower(i)
print "Convert into upper case :"toupper(i)
#print "Convert String into number :"strtonum("123abc")
}

Run:

awk -f strfun1.awk

Output:

String : TYBCA SEM 5


String length :11
Return specified string :BCA S
Return the specified string position :8
Divided String into array :3
TYBCA
SEM
5
Convert into lower case :tybca sem 5
Convert into upper case :TYBCA SEM 5

Arithmetic Operation

File name: arith1.awk

BEGIN{
i=4.16;
print "Cosine Value :"cos(i)
print "sine Value :"sin(i)
print "natural logarithm Value :"cos(i)
print "exponent of i :"exp(i)
print "integer part :"int(i)
print "square of i :"sqrt(i)
}

output :

Cosine Value :-0.524722


sine Value :-0.851273
natural logarithm Value :-0.524722
exponent of i :64.0715
integer part :4
square of i :2.03961

How to use System function in awk :

filename: system1.awk

BEGIN{
{system ("date")}
{system ("ls")}
}
output:

Fri Sep 2 12:39:42 IST 2022

aa1.awk awk2 function1.awk matrixadd.awk while.awk


arith1.awk awk2.awk getline1 matrix.awk
awk1 dowhile1.awk getline1.awk strfun.awk
awk1.awk for1.awk getline2.awk system1.awk

You might also like