EmployeesList_awk
EmployeesList_awk
ID@NAME@AGE@DEPARTMENT@SALARY
1@Sanjay@25@Sales@50000
2@Mahesh@30@Marketing@70000
3@Amulya@55@HR@35000
4@Devi Priya@40@Sales@60000
5@Sakshi@34@HR@80000
6@Priyadarshan@50@Marketing@95000
7@Vennela@48@Sales@110000
8@Rajesh@52@HR@40000
Solution :
awk 'BEGIN{FS="@";}
{
if(NR>1)
{
print $2;
}
}' input.txt
Solution:
awk 'BEGIN{FS="@";}
{
if(NR>1)
{
print $2;
}
}' input.txt | sort
Situation : Display all employees who are working in sales and sorting the result
in descending order
Solution:
awk 'BEGIN{FS="@";}
{
if($4 == "Sales")
{
print $0;
}
}' input.txt | sort -r
Situation : Find employees with salary greater than 60000. Only Print Name and
Salary column by separating fields with '@'
Solution:
awk 'BEGIN{FS="@";OFS="@";}
{
if($5 > 60000)
{
print $2, $5;
}
}' input.txt
Solution:
awk 'BEGIN{FS="@";count=0;}
{
if($4 == "HR")
{
count++;
}
}
END{
print "Total No.of employees from HR department is", count;
}' input.txt
Solution:
awk 'BEGIN{FS="@";sum=0;}
{
sum = sum + $5;
}
END{
print "Total salary is", sum;
}' input.txt
Situation : Display names and ages of employees older than 40 by skipping the
header
Solution:
awk 'BEGIN{FS="@";}
{
if($3 > 40 && NR > 1)
{
print $2, $3;
}
}' input.txt
Solution :
awk 'BEGIN{FS="@";sum=0;count=0;}
{
if($4 == "Marketing")
{
sum = sum + $5;
count++;
}
}
END{
print "The average salary from marketing department is", sum/count;
}' input.txt
Situation : Find Maximum salary of employee. Print the Name, age and salary
Solution:
Solution:
awk 'BEGIN{FS="@";}
{
if(NR > 1)
{
print $0;
}
}' input.txt | sort -t@ -k 5n | awk 'BEGIN{FS="@";}
{
if (NR == 1)
{
print $2, $5;
}
}'
Solution:
awk 'BEGIN{FS="@";}
{
if(NR > 1)
{
newSalary = $5 * 1.1;
Solution :
awk 'BEGIN{FS="@";}
{
if(NR <= 3)
{
print $0;
}
}' input.txt
Situation : Display employees whose name start with 'S'
Solution :
awk 'BEGIN{FS="@";}
{
if($2 ~ /^S/)
{
print $0;
}
}' input.txt
Solution :
awk 'BEGIN{FS="@";}
{
if($2 ~ /a$/)
{
print $0;
}
}' input.txt
Solution :
awk 'BEGIN{FS="@";}
{
if($2 ~ /y/)
{
print $0;
}
}' input.txt
Solution :
awk 'BEGIN{FS="@";}
{
if($2 ~ /[AEIOU]/)
{
print $0;
}
}' input.txt