Friday 26 October 2012

awk database

#Title: Write a  awk program to handle student data base with options given below,
#    a) Create data base. b) View Data Base. c) Insert a record.
#    d) Delete a record. e) Modify a record. f) Result of a particular
#       student. g) Exit.

BEGIN{
main();
}
function main()
{
   
        printf("\n1. Insert")
        printf("\n2. View")
        printf("\n3. Search")
        printf("\n4. Modify")
        printf("\n5. Delete")
        printf("\n6. Result Analysis")
        printf("\n7. Exit")
        printf("\nPlease enter your choice\t")
        getline choice
       
        if(choice==1)
        {
            printf("\nEnter your roll number\t")
            getline roll_no
            printf("\nEnter the name\t")
            getline name
            printf("\nEnter the marks")
            printf("\nDBMS : ")
            getline dbms
            printf("SE : ")
            getline se
            printf("CNT : ")
            getline cnt
            printf("OS : ")
            getline os
            printf("TOC : ")
            getline toc
            printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",roll_no,name,dbms,se,cnt,os,toc)>>"student.txt"
            #printf("\n%d|%s",rollno,name)>>"stud1.txt";
        }
       
        else if(choice==2)
        {
            printf("\nRoll No\tName\tDBMS\tSE\tCNT\tOS\tTOC")
            printf("\n=======================================================================")
            while(getline < "student.txt"==1)
            {
                printf("\n%d\t%s\t%d\t%d\t%d\t%d\t%d",$1,$2,$3,$4,$5,$6,$7)
            }
        }

        else if(choice==3)
        {
            printf("\nEnter the roll number to be searched\t")
            getline roll_no
            flag=0
            while(getline < "student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\n%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)
                    flag=1
                    break
                }
            }
            if(flag==0)
                printf("\nRecord not found\n")
        }

        else if(choice==4)
        {
            printf("\nEnter the roll number to be modified\t")
            getline roll_no
            flag=0
            while(getline < "student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\nEnter new roll number\t")
                    getline rno
                    printf("\nEnter the new name\t")
                    getline name
                    printf("\nEnter the new marks")
                    printf("\nDBMS : ")
                    getline dbms
                    printf("SE : ")
                    getline se
                    printf("CNT : ")
                    getline cnt
                    printf("OS : ")
                    getline os
                    printf("TOC : ")
                    getline toc
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",roll_no,name,dbms,se,cnt,os,toc)>>"temp.txt"
                    flag=1
                }
                else
                {
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)>>"temp.txt"
                }
            }
            "mv temp.txt student.txt"|getline
            #"rm temp.txt"|getline

            if(flag==1)
                printf("\nRecord modified successfully\n")
            else
                printf("\nRecord not found\n")
        }
       
        else if(choice==5)
        {
            printf("\nEnter the roll number to be deleted\t")
            getline roll_no
            flag=0
            while(getline<"student.txt"==1)
            {
                if($1!=roll_no)
                {
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)>>"temp.txt"
                }
                else
                    flag=1
            }
            "mv temp.txt student.txt"|getline
       
            if(flag==0)
                printf("\nRecord not found\n")
            else
                printf("\nRecord deleted sucessfully\n")
        }
       
        else if(choice==6)
        {
            printf("\nEnter the roll number\t")
            getline roll_no
            flag=0
            while(getline<"student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\nTotal = %d",($3+$4+$5+$6+$7))
                printf("\nPercentage = %2.2f ",($3+$4+$5+$6+$7)/5)
                    flag=1
                    break
                }
            }
           
            if(flag==0)
                printf("\nRecord not found")
        }

        else if(choice==7)
        {
            exit 0
        }
        printf("\n")
}       

No comments:

Post a Comment