I am facing trouble just with modify_record function in the code provided below. I could use awk but my instructor told us to do it using sed. Could anyone help in this?
menu() {
echo "choose one of the following options: n"
echo "1) create a student database"
echo "2) view a student database"
echo "3) insert a student record"
echo "4) delete a student record"
echo "5) modify a record"
echo "6) display result"
echo ""
}
create_db() {
echo "creating a student database..."
touch student.db
echo "created a student database."
}
view_db() {
echo -e "opening database... n"
cat student.db
}
insert_record() {
echo -e "enter roll number: c"
read roll
echo -e "enter name of student: c"
read name
echo -e "enter marks of subject 1: c"
read sub1
echo -e "enter marks of subject 2: c"
read sub2
echo -e "enter marks of subject 3: c"
read sub3
echo "$roll|$name|$sub1|$sub2|$sub3" >> student.db
}
delete_record() {
echo -e "enter roll number whose info is to be deleted: c"
read roll
grep -iv $roll student.db > temp.db && mv temp.db student.db
}
modify_record() {
echo -e "enter roll no. whose info is to be modified: c"
read roll
#echo -e "enter name: "
#read temp_name
echo -e "enter sub1 marks: c"
read newsub1
echo -e "enter sub2 marks: c"
read newsub2
echo -e "enter sub3 marks: c"
read newsub3
sed -i "s/^$roll|([^|]*)|[^|]*|[^|]*||[^|]*$/&1|$newsub1|2|$newsub2|3|$newsub3/" student.db
}
calc_avg() {
echo "enter roll no. whose average is to be calculated: c"
read rollno
x=`grep $rollno student.db | cut -d'|' -f3`
y=`grep $rollno student.db | cut -d'|' -f4`
z=`grep $rollno student.db | cut -d'|' -f5`
sum=`expr $x + $y + $z`
average=`expr $sum / 3`
# echo -e "average: $averagen"
echo -e "average: $averagen"
}
while :
do
menu
read -p "enter your choice: " choice
case $choice in
1) create_db ;;
2) view_db ;;
3) insert_record ;;
4) delete_record ;;
5) modify_record ;;
6) calc_avg ;;
7) break ;;
*) echo "invalid choice"
esac
done
The database which is created when you run this code looks like this:
rollno|name|sub1|sub2|sub3
The sed line in modify_record(), rather than replacing the subject marks it inserts them randomly in between other fields. I looked up other solutions and tried to copy their implementation but it didn’t work.
Keklord is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.