postheadericon unix grep command


Definition : Write a script that deletes all leading and trailing spaces in all lines in a file. Also remove blank lines from a file. Locate lines containing only printf but not fprintf

File Name : ex27.sh


clear
echo "Enter file name : "
read fileName
if [ -f $fileName ]
then
echo "____________________________________"
        echo " Trim File .... "
        echo "____________________________________"

        # Delete leading & trailing whitespace from each line and store it tempFile.txt
        sed 's/^[ \t]*//;s/[ \t]*$//;' $fileName >tempFile.txt

        # Remove all blank lines from tempFile.txt
        sed '/^$/d' tempFile.txt

        # Replace content of this two file
        mv tempFile.txt $fileName
else
        echo "File Not Found"
fi

echo "____________________________________"
echo "  Located Lines .... "
echo "____________________________________"

# Locate lines containing only printf but not fprintf
grep -e "printf" $fileName | grep -v "fprint"

postheadericon Write a script to learn command line argument


Definition : Write a script that behaves both in interactive and non-interactive mode. When no arguments are supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts for deletion of the file. If the user supplies arguments with the script, then it works on those files only.

File Name : ex26.sh


flag=1
if [ $# -ne 0 ]
then
            # if user provide arguments
             echo $* >temp
            flag=1
else
             # if user not provide arguments
            ans=`ls *.c`
            if [ "" == "$ans" ]
            then
                        echo "There are no c file"
                        flag=0
            else
                        ls *.c>temp
                        flag=1
            fi
fi
if [ $flag -eq 1 ]
then
            for filename in `cat temp`
            do
                        if [ -f $filename ]
                        then
                                    echo "_______________ $filename _______________"
                                    sed -n -e "1,10 p" $filename
                                    rm -i $filename
                        fi
            done
rm temp
fi


postheadericon Write a script to display the last modified file


File Name : ex24.sh


clear
echo "Enter Directory Name :\c"
read dir1

cd $dir1

ls -lt | head -2 | tail -1 |cut -d " " -f 10


postheadericon Write a script to display all lines of a file in ascending order


File Name : ex23.sh


clear
echo "Enter File Name :\c"
read filename
sort $filename


postheadericon Write a script to display all words of a file in ascending order.


File Name : ex22.sh


echo "Enter File name:\c"
read fn
 for i in `cat $fn`
do
echo $i >> f2.txt
done
sort f2.txt
rm f2.txt


postheadericon Write a script to check whether a given number is palindrome or not


File Name : ex21.sh


clear
echo "Enter No : \c"
read no

m=$no
rev=0

while [ $no -gt 0 ]
do
            r=`expr $no % 10`
            rev=`expr $rev \* 10 + $r`
            no=`expr $no / 10`
done

if [ $m = $rev ]
then
            echo " [ $m] is Palindrome"
else
            echo " [ $m ] is not Palindrome"
fi


postheadericon Write a script to check whether a given string is palindrome or not


File Name : ex20.sh


clear
echo "Enter String : \c"
read s
p=$s

m=`expr $s | wc -c`
i=`expr $m - 1`

while [ $i -ge 1 ]
do
            k=`expr $s | cut -c$i`
            r=$r$k
            i=`expr $i - 1`
            echo $k
done
            echo $r
if [ "$p" != "$r" ]
then
            echo "It is not palindrome"
else
            echo "It is palindrome"
fi


postheadericon Write a shell script to mathematics calculation


Definition: Write a script to calculate gross salary for any number of employees

                                   Gross Salary =Basic + HRA + DA.
                                   HRA=10% and DA= 15%.

File Name : ex19.sh



clear
echo "Enter Basic Salary :\c"
read bs
if [ $bs -lt 1500 ]
then
  hra=`echo $bs \* 10 / 100 | bc`
  da=`echo $bs \* 15 /100 | bc`
else
   hra=500
   da=`echo $bs \* 98/100 | bc`
fi
gs=`echo $bs + $hra + $da | bc`

echo " DA : $da"
echo " HRA :  $hra"
echo "GROSS SALARY : $gs"


postheadericon Write a shell script to Perform Following String Operations


Definition : Write A Script To Perform Following String Operations Using Menu: 

                       1. COMPARE TWO STRINGS.
                       2. JOIN TWO STRINGS.
                       3. FIND THE LENGTH OF A GIVEN STRING.
                       4. OCCURRENCE OF CHARACTER AND WORDS
                       5. REVERSE THE STRING.

File Name : ex18.sh


clear
choice=y
while [ "$choice" = "y" ]
do
echo "____________________________________________"
echo "1. COMPARE TWO STRINGS"
echo "2. JOIN TWO STRINGS"
echo "3. FIND THE LENGTH OF A GIVEN STRING"
echo "4. OCCURRENCE OF CHARACTER AND WORDS"
echo "5. REVERSE THE STRING"
echo "6. EXIT"
echo "____________________________________________"
echo "Enter Choice: \c"
read ch
echo "____________________________________________"
case $ch in
1)
        echo "Enter String1: \c"
        read str1
        echo "Enter String2: \c"
        read str2

        if [ $str1 = $str2 ]
        then
                echo "String is equal"
        else
                echo "String is not equal"
        fi
        ;;
2)
        echo "Enter String1: \c"
        read str1
        echo "Enter String2: \c"
        read str2

        str3=$str1$str2
        echo "Join String: \c" $str3
        ;;
3)
        len=0
        echo "Enter String1: \c"
        read str1
            len=$(echo "$str1" | wc -c)
            len=`expr $len - 1`
        echo "Length: " $len
        ;;
4)
        echo "Enter String: \c"
        read str

        echo "Enter Word to find : \c"
        read word

        echo $str | cat > str1.txt

        grep -o $word str1.txt | cat > str2.txt
        count=`grep -c $word str2.txt`
            echo "Count:  \c"$count
    ;;
5)
        echo "Enter String1: \c"
        read str

        len=`expr $str | wc -c`
        len=`expr $len - 1`
        while [ $len -gt 0 ]
        do
                rev=`expr $str | cut -c $len`
                ans=$ans$rev
                len=`expr $len - 1`
        done
        echo "Reverse String: \c"$ans
        ;;

6)         exit      ;;

*)         echo "Invalid Choice ....."                    ;;
            esac
 echo "Do u want to continue.....? [y/n]"
 read choice
 case $choice in
  Y|y) choice=y;;
  N|n) choice=n;;
  *) choice=y;;
 esac
done

 

postheadericon Write a Shell Script for Simple Database Management System Operation


Write a Script for Simple Database Management System Operation.

Database File Contains Following Fields.
            EMP_NO
            EMP_NAME
            EMP_ADDRESS
            EMP_AGE
            EMP_GENDER
            EMP_DESIGNATION
            EMP_BASIC_SALARY

Provide Menu Driven Facility For
            VIEW RECORD BASED ON QUERY
            ADD RECORD
            DELETE RECORD
            MODIFY RECORD.
            COUNT TOTAL NUMBER OF RECORDS
            EXIT


File Name : Employee.txt


1,bipin,xyz,27,Male,Professor,10000,true
2,hitesh,xyz,27,Male,Accountant,10000,false
3,ritesh,xyz,27,Male,HOD,20000,true
4,mitesh,xyz,27,Male,Clerk,2000,true
5,jitesh,xyz,27,Male,DBA,5000,true

File Name : ex17.sh


clear
AutoNumber()
{
            local eno=0     
            f=0
            for j in `cat Employee.txt`
            do
                        eno=$(echo "$j" | cut -d "," -f 1)
                        f=1
            done
            if [ $f = 1 ]
            then
                        eno=`expr $eno + 1`
            else
                        eno=1
            fi
            echo $eno
}



Insert()
{
            clear
           
            eno=$1           
            echo "Enter Employee No: $eno"
           
            echo "Enter Employee Name: \c"
            read enm

            echo "Enter Employee Address: \c"
            read eadd
           
            echo "Enter Employee Age : \c"
            read eage

            echo "Enter Employee Gender: \c"
            read egen
           
            echo "Enter Employee Designation : \c"
            read edes


            echo "Enter Employee Basic Salary : \c"
            read ebal

           
            echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,true" >> Employee.txt

            echo "                 Insert Sucessfully                           "

}

Display()
{
            clear
            echo "__________________________________________________"                       
            echo "                              Employee Details "
            echo "__________________________________________________"                       
            echo "__________________________________________________"                       
            echo "#ENO \t ENAME \t\t EADDR \t\t\t EAGE \t EGEN \t EDES \t\t EBAL"    
        
            for j in `cat Employee.txt`
            do
                        eno=$(echo "$j" | cut -d "," -f 1)
                        enm=$(echo "$j" | cut -d "," -f 2)
                        eadd=$(echo "$j" | cut -d "," -f 3)
                        eage=$(echo "$j" | cut -d "," -f 4)
                        egen=$(echo "$j" | cut -d "," -f 5)
                       
                        edes=$(echo "$j" | cut -d "," -f 6)
                        ebal=$(echo "$j" | cut -d "," -f 7)
                        tfval=$(echo "$j" | cut -d "," -f 8)
                        if [ $tfval = "true" ]
                        then
                                  echo "___________________________________________"
                                  echo "$eno \t $enm \t\t $eadd \t\t $eage \t $egen \t $edes \t $ebal"
                        fi
            done   
            echo "__________________________________________________"                       
}

Search()
{
            clear

            echo "Enter Employee NO: \c"
            read no

            echo "__________________________________________________"                       
            echo "                 Employee Details                       "
            echo "__________________________________________________"                       
            flag=0
            for j in `cat Employee.txt`
            do
                        eno=$(echo "$j" | cut -d "," -f 1)
                        enm=$(echo "$j" | cut -d "," -f 2)
                        eadd=$(echo "$j" | cut -d "," -f 3)
                        eage=$(echo "$j" | cut -d "," -f 4)
                        egen=$(echo "$j" | cut -d "," -f 5)
                        edes=$(echo "$j" | cut -d "," -f 6)
                        ebal=$(echo "$j" | cut -d "," -f 7)
                        tfval=$(echo "$j" | cut -d "," -f 8)
                                               
                        if [ $no -eq $eno ] && [ $tfval = "true" ]
                        then
                                    flag=1
                                 echo "________________________________________"                        
                                    echo "  ENo : $eno                      EName : $enm            "  
                                    echo "________________________________________"                        
                                    echo "  EAdd                    :                      $eadd   "
                                    echo "  EAge                    :                      $eage   "
                                    echo "  EGen                    :                      $egen   "
                                    echo "________________________________________"                        
                                    echo "  EDes                    :                      $edes   "
                                    echo "________________________________________"                        
                                    echo "  ESal                      :                      $ebal   "
                                    echo "________________________________________"                        
                        fi
            done
            if [ $flag = 0 ]
            then
                 echo "               No Record Found              "
            fi
            echo "__________________________________________________"                          

}
Delete()
{
            clear
            f=0
            echo "Enter Employee NO: \c"
            read no

            for j in `cat Employee.txt`
            do
                        eno=$(echo "$j" | cut -d "," -f 1)
                        enm=$(echo "$j" | cut -d "," -f 2)
                        eadd=$(echo "$j" | cut -d "," -f 3)
                        eage=$(echo "$j" | cut -d "," -f 4)
                        egen=$(echo "$j" | cut -d "," -f 5)
                        edes=$(echo "$j" | cut -d "," -f 6)
                        ebal=$(echo "$j" | cut -d "," -f 7)
                       
                        if [ $no -eq $eno ]
                        then
                                    f=1                              
                                    line=$(echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,false")
                                    fnm=`cat Employee.txt`
                                    d=$(echo "$fnm" | sed s/$j/$line/g )
                                    echo $d > Employee.txt          
                                    echo "                 Delete Successfully                           "
                        fi
            done
            if [ f = 0 ]
            then
                          echo "               No Record Found              "
            fi
}

Update()
{
            clear

            echo "Enter Employee NO: \c"
            read no


                       
            for j in `cat Employee.txt`
            do
                                                eno=$(echo "$j" | cut -d "," -f 1)
                        enm=$(echo "$j" | cut -d "," -f 2)
                        eadd=$(echo "$j" | cut -d "," -f 3)
                        eage=$(echo "$j" | cut -d "," -f 4)
                        egen=$(echo "$j" | cut -d "," -f 5)
                        edes=$(echo "$j" | cut -d "," -f 6)
                        ebal=$(echo "$j" | cut -d "," -f 7)

                       
                        if [ $no -eq $eno ]
                        then
                                    echo "______________Enter New Record______________"
                                    echo "Enter Employee No: $eno"
           
                                    echo "Enter Employee Name: \c"
                                    read enm

                                    echo "Enter Employee Address: \c"
                                    read eadd
           
                                    echo "Enter Employee Age : \c"
                                    read eage
           
                                    echo "Enter Employee Gender: \c"
                                    read egen
           
                                    echo "Enter Employee Designation : \c"
                                    read edes

                                    echo "Enter Employee Basic Salary : \c"
                                    read ebal

           
                                    line=$(echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,true")

                                    #line=$(echo "$eno,$snm,$m1,$m2,$m3,$total,$per,true")
                                    fnm=`cat Employee.txt`
                                    d=$(echo "$fnm" | sed s/$j/$line/g )
                                    echo $d > Employee.txt          
                                   
                                    echo "                 Update Sucessfully                           "

                        fi
            done
}




while [ true ]
do
echo " _______________________________"
echo " 1. Insert  "
echo " 2. Delete  "
echo " 3. Update  "
echo " 4. Display "
echo " 5. Search  "
echo " 6. Exit    "
echo " _______________________________" 
echo "Enter Choice: \c"
read ch

case $ch in

            1)
               nxtSrNo=$(AutoNumber)
               Insert $nxtSrNo
               ;;
            2) Delete ;;
            3) Update ;;
            4) Display ;;
            5) Search ;;
            6) break;;
            *) echo " Wrong Choice "
esac
done

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.