I am learning shell scripting and have a following requirement:
Req.:
Need to collect the grades from a students grade file and create files having respective grades in it.
Sample file:
eng_student_grades.csv
id | div | grd
001| ER | B
002| JI | C
003| ER | B
004| LO | A
005| LO | A
006| KI | D
007| JI | A
Wrote this following piece of code in shell:
$source_table = student_rcrd
$filename = eng_student_grades.csv
$target_loc = /home/grades
if echo "$source_table" | grep 'STDNT' >> $JOBLOG; then
for filename in `ls`;
do
if cat "$filename" | grep 'A'; then
target_dir="GRD_A"
elif cat "$filename" | grep 'B'; then
target_dir="GRD_B"
elif cat "$filename" | grep 'C'; then
target_dir="GRD_C"
elif cat "$filename" | grep 'D'; then
target_dir="GRD_D"
else
continue
fi
newfilename=$target_dir"_"$filename".txt"
sudo cp $filename $newfilename
sudo zip $newfilename.zip $newfilename
But the code is not producing the expected results.
Actual O/P:
GRD_A_eng_student_grades.txt
Expected O/P:
GRD_A_eng_student_grades.txt
GRD_B_eng_student_grades.txt
GRD_C_eng_student_grades.txt
GRD_D_eng_student_grades.txt
Not getting where I'm making mistake. Any help to resolve the issue would be really helpful. Thanks in advance for your time.