Grep(global regular expression print) : Search string in the File : Example 1

: '
grep (global regular expression print).
The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern.

Options Description

-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.

-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line, with each such part on a separate output line.
-A n : Prints searched line and n lines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.
'



 [prabhucloudxlab@cxln4 ~]$ cat filegrep.txt
This is linux
This is windows
This is MAC
This is linux
This is windows
This is MAC
This is linux
This is windows
This is MAC
This is linux
This is windows
This is MAC
This is linux
This is windows
This is MAC
This is linux
This is windows
This is MAC
This is linux
This is windows
This is MAC
This is linux
This is windows
This is MAC


========================================================================
#! bin/bash
# File exist or not check

[adimulamvenkat19851609@cxln4 ~]$ cat>hello.sh
echo " enter filename is search text from "
read filename

if [[ -f $filename ]]
then
        echo "enter the text1 to search"
        read grepvar1
        grep $grepvar1 $filename
else
        echo "$filename does not exist"
fi


[prabhucloudxlab@cxln4 ~]$ sh hello.sh
 enter filename is search text from
filegrep.txt
enter the text1 to search
linux
This is linux
This is linux
This is linux
This is linux
This is linux
This is linux
This is linux
This is linux



[prabhucloudxlab@cxln4 ~]$ sh hello.sh
enter filename is search text from
filegrep.txt
enter the text to search
Linux


Note: 
Linux..
L is uppercase... so empty output... for this we need to use (-i) for case sensitive.