Professional Documents
Culture Documents
Awk Notes
Awk Notes
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
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
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
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
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
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:
Arithmetic Operation
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 :
filename: system1.awk
BEGIN{
{system ("date")}
{system ("ls")}
}
output: