0% found this document useful (0 votes)
3 views4 pages

EmployeesList_awk

The document provides various awk commands to manipulate and analyze employee data stored in a specific format. It includes solutions for displaying employee names, sorting, filtering by department and salary, calculating totals and averages, and finding maximum and minimum salaries. Each situation is paired with a corresponding awk command to achieve the desired outcome.

Uploaded by

pushpavallik2610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

EmployeesList_awk

The document provides various awk commands to manipulate and analyze employee data stored in a specific format. It includes solutions for displaying employee names, sorting, filtering by department and salary, calculating totals and averages, and finding maximum and minimum salaries. Each situation is paired with a corresponding awk command to achieve the desired outcome.

Uploaded by

pushpavallik2610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Input:

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

Situation : Display the names of all employees by removing the header

Solution :

awk 'BEGIN{FS="@";}
{
if(NR>1)
{
print $2;
}
}' input.txt

Situation : Sort the employee names in ascending order

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

Situation : Display the Total No. of Employees from HR Department

Solution:

awk 'BEGIN{FS="@";count=0;}
{
if($4 == "HR")
{
count++;
}
}
END{
print "Total No.of employees from HR department is", count;
}' input.txt

Situation : Calculate the total salary of all employees

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

Situation : # Find Average Salary employees from marketing department

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:

sort -t@ -k 5n input.txt | awk 'BEGIN{FS="@";}


END{
print $2, $3, $5;
}'

Situation : Find Minimum salary of employee. Print Name and Salary

Solution:

awk 'BEGIN{FS="@";}
{
if(NR > 1)
{
print $0;
}
}' input.txt | sort -t@ -k 5n | awk 'BEGIN{FS="@";}
{
if (NR == 1)
{
print $2, $5;
}
}'

Situation : Print the employees with their salary incremented by 10%

Solution:

awk 'BEGIN{FS="@";}
{
if(NR > 1)
{
newSalary = $5 * 1.1;

print $2, newSalary;


}
}
' input.txt

Situation : Sort the employees by salary in ascending order

Solution : awk '{if(NR>1) print $0}' input.txt | sort -t@ -k 5n

Situation : Display only first 3 rows

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

Situation : Display employees whose name ends with 'a'

Solution :

awk 'BEGIN{FS="@";}
{
if($2 ~ /a$/)
{
print $0;
}
}' input.txt

Situation : Display employees whose name contains letter 'y'

Solution :

awk 'BEGIN{FS="@";}
{
if($2 ~ /y/)
{
print $0;
}
}' input.txt

Situation : Display employees whose name contains an vowel character

Solution :

awk 'BEGIN{FS="@";}
{
if($2 ~ /[AEIOU]/)
{
print $0;
}
}' input.txt

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy