Monday, January 20, 2014

Unix and Shell Programming - Grep Command

with 0 Comment
clear
echo "Enter the file name"
read f
echo
cat $f
while true
do
echo "Grep Command"
echo "----------------"
echo
echo "Menu"
echo "-----"
echo
echo "1.Inverse"
echo
echo "2.Ignore"
echo
echo "3.File Name"
echo
echo "4.Line Number"
echo
echo "5.Count"
echo
echo "6.Multiple Search Options"
echo
echo "7.Exit"
echo
echo "Enter the choice"
read ch
case $ch in

1)
echo "Inverse"
echo "-------"
echo
echo "Contents of the file"
cat $f
echo "Enter the Search pattern"
read p
echo
grep -v $p $f
;;

2)
echo "Ignore"
echo "------"
echo
echo "Contents of the file"
cat $f
echo "Enter the Search pattern"
read p
echo
grep -i $p $f
;;

3)
echo "File Name"
echo "-----------"
echo
echo "Contents of the file"
cat $f
echo "Enter the Search pattern"
read p
echo
grep -l $p $f
;;

4)
echo "Line Number"
echo "--------------"
echo
echo "Contents of the file"
cat $f
echo "Enter the Search pattern"
read p
echo
grep -n $p $f
;;

5)
echo "Count Option"
echo "--------------"
echo
echo "Contents of the file"
cat $f
echo "Enter the Search pattern"
read p
echo
grep -c $p $f
;;

6)
echo "Multiple Search Options"
echo "-------------------------"
echo
echo "Contents of the file"
cat $f
echo "Enter the Search pattern1"
read p1
echo "Enter the Search pattern2"
read p2
echo
grep -e $p1 -e $p2 $f
;;

7)
echo "Exit"
echo "----"
exit
;;
esac
done


----------
OUTPUT:
----------
Enter the file name
cmp

Content of the file
------------------
madurai
thrichy
chennai
delhi
banglore

grep command
---------------
Menu
------
1.Inverse
2.Ignore
3.File Name
4.Line Number
5.Count
6.Multiple Search Options
7.Exit

Enter the Choice
1
Inverse
-------
Contents of the file
-------------------
madurai
thrichy
chennai
delhi
banglore

Enter the Search pattern
delhi

Content of the file
------------------
madurai
thrichy
chennai
banglore

Enter the choice
2

Ignore
-------
Contents of the file
-------------------
madurai
thrichy
chennai
delhi
banglore

Enter the seach pattern
CHENNAI

chennai

Enter the choice
3

File Name
-----------
Contents of the file
-------------------
madurai
thrichy
chennai
delhi
banglore

Enter the search pattern
thrichy

cmp

Enter the Choice
4

Line Number
--------------
Contents of the File
--------------------
madurai
thrichy
chennai
delhi
banglore

Enter the seach pattern
chennai

3:chennai

Enter the choice
5

Count Option
--------------
Contents of the file
-------------------
madurai
thrichy
chennai
delhi
banglore

Enter the search pattern
banglore

1
Enter the choice
6

Multiple Search Options
-------------------------
Contents of the file
-------------------
madurai
thrichy
chennai
delhi
banglore

Enter the search pattern1
thrichy
Enter the search pattern2
delhi

thrichy
delhi

Enter the choice
7
Exit

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive