Consider a file “data.txt” which contains several records. Each record contains ID(7chars), 1 space, First name (10 chars),1 space, Second name (8 chars), 1space, marks in physics( 3chars), 1space, marks in chemistry(3 chars), 1space, marks in mathematics(3 chars) and anewline character. Sample file content is as shown below:
71723 Ram Sen 70 72 75
91924 Raghubir Yadav 82 73 80
53425 Ram Chauhan 93 81 86
44917 Ratan Yadav 95 79 91
Write a shell program info.sh to achieve the following
i) If the programis run without any argument ($./info.sh), it has to display the first name, second name and average score of each student in the file.
#!/bin/bash
if [[ $# > 0 ]]; then
echo "Command line arguments are not supported"
exit 1
fi
sourceFile="data.txt"
while IFS= read -r line
do
firstName=${line:8:10}
secondName=${line:19:8}
physicsMarks=${line:28:3}
chemistryMarks=${line:32:3}
mathematicsMarks=${line:36:3}
avg=$(((${physicsMarks} + ${chemistryMarks} + ${mathematicsMarks}) / 3))
printf '%s %s %s\n' ${firstName} ${secondName} ${avg}
done < "$sourceFile"
Comments
Leave a comment