You are on page 1of 8

Operating System -6CS103

2023-24
Name:Yash Lalchandani
Rollno :23MCA027
Practical_5

Create a file employees.txt with following fields

Emp_ID | Emp_Name | Designation | Department | Salary | Age

001 | Kanti | Clerk | Product | 250000 | 20

002 | Sameer Kumar Rai | Manager | HR | 50000 | 25

003 | Anurag Saxena | Technician | Server | 45000 | 21

004 | Tushar Chuggani | Manager | Sales | 30000 | 25

005 | Vedant Saksena | Programmer | App Building | 150000 | 24

Command:- cat employees.txt


Output:-

1. Remove all the blank lines from the file

Command:- grep -v '^$' employees.txt

Output:-

2. locate all lines containing "saxena" and "saksena"

Command:- grep -i 'saxena\|saksena' employees.txt

Output:-

3. locate all lines longer than 15 characters

Command:- awk 'length($0) > 15' employees.txt


Output:-

4. Generate list of all ordinary files in your current directory that are
not writable?

Command:- ls -l|awk '($1 ~/^.*w.*/)'

Output:-

5. Find out the name and designation of the youngest person who is
not 'Manager'

Command:- grep -v "Manager" employees.txt | sort -t '|' -k6n | head -n


4

Output:-

6. write a shell script which should do the following task

Should read a file emp.dat having field as

Emp_Id,Name,Dept,Age,Gender,designation,Basic
Command:-
echo "Emp_Id,Name,Dept,Age,Gender,Designation,Basic" > emp.dat
echo "101,kamal,Clerk,22,Male,Clerk,30000" >> emp.dat
echo "102,jeet,Clerk,21,Female,Clerk,32000" >> emp.dat
echo "201,yash,Manager,25,Male,Manager,45000" >> emp.dat
echo "202,deepa,Manager,22,Female,Manager,50000" >> emp.dat
echo "301,jay,Director,25,Male,Director,60000" >> emp.dat
echo "302,gaurav,Director,20,Female,Director,70000" >> emp.dat

cat emp.dat

Output:-

Generate separate file for each department and add net salary field
and give increment as follows:

clerk = Basic + HRA(1000) - 5% IT of Basic


Manager= Basic + HRA(2000) - 10% IT of Basic
Director= Basic + HRA (5000) + sp. inc(20000) - 20% IT of Basic

Command:- sh script.sh
#!/bin/bash

while IFS='|' read -r Emp_Id Name Dept Age Gender Designation


Basic; do
case "$Dept" in
"Clerk")
HRA=1000
IT_PERCENT=5
;;
"Manager")
HRA=2000
IT_PERCENT=10
;;
"Director")
HRA=5000
SP_INC=20000
IT_PERCENT=20
;;
*)
echo "Unknown department: $Dept. Skipping."
continue
;;
esac

IT_AMOUNT=$(bc <<< "scale=2; $Basic * $IT_PERCENT / 100")


Net_Salary=$(bc <<< "scale=2; $Basic + $HRA - $IT_AMOUNT")

Department_File="${Dept}_file.dat"

if [ ! -e "$Department_File" ]; then
echo "Emp_Id|Name|Dept|Age|Gender|Designation|Basic|
Net_Salary" > "$Department_File"
fi

echo "$Emp_Id|$Name|$Dept|$Age|$Gender|$Designation|$Basic|
$Net_Salary" >> "$Department_File"
done < emp.dat

echo "Separate department files have been generated."

Output:-
Generate the report of each department for all the employees and
count the number of clerks,managers and Directors

Command:- sh script2.sh
#!/bin/bash

# Initialize counters
clerk_count=0
manager_count=0
director_count=0

# Read the file line by line


while IFS=',' read -r Emp_Id Name Dept Age Gender Designation
Basic; do
case "$Designation" in
"Clerk")
((clerk_count++))
;;
"Manager")
((manager_count++))
;;
"Director")
((director_count++))
;;
*)
exit 1
;;
esac

# Calculate net salary based on the designation


case "$Designation" in
"Clerk")
HRA=1000
tax=$((Basic * 5 / 100))
net_salary=$((Basic + HRA - tax))
;;
"Manager")
HRA=2000
tax=$((Basic * 10 / 100))
net_salary=$((Basic + HRA - tax))
;;
"Director")
HRA=5000
special_increment=20000
tax=$((Basic * 20 / 100))
net_salary=$((Basic + HRA + special_increment - tax))
;;
esac

# Append to the respective department file


echo "Emp_Id: $Emp_Id, Name: $Name, Dept: $Dept, Age: $Age,
Gender: $Gender, Designation: $Designation, Basic: $Basic, Net
Salary: $net_salary" >> "${Dept}_file.dat"

done < emp.dat

# Display the report for each department


echo "Clerk Count: $clerk_count"
echo "Manager Count: $manager_count"
echo "Director Count: $director_count"
Output:-

You might also like