The grep filter searches a file or pipe output for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for globally search for regular expression and print out). Syntax:
grep [options] pattern [files]
or
| grep [options] pattern
Options Description
-c : This prints only a count of the lines that match a pattern
-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.
-v : This prints out all the lines that do not matches the pattern
-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 nlines 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.
Sample Commands
Consider the below file as an input.
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
$grep -i "UNix" geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
$grep -c "unix" geekfile.txt
Output:
2
$grep -l "unix" *
or
$grep -l "unix" f1.txt f2.txt f3.xt f4.txt
Output:
geekfile.txt
$ grep -w "unix" geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
$ grep -o "unix" geekfile.txt
Output:
unix
unix
unix
unix
unix
unix
$ grep -n "unix" geekfile.txt
Output:
1:unix is great os. unix is opensource. unix is free os.
4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
$ grep -v "unix" geekfile.txt
Output:
learn operating system.
Unix linux which one you choose.
$ grep "^unix" geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
$ grep "os$" geekfile.txt
10.Specifies expression with -e option. Can use multiple times :
$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" geekfile.txt
$cat pattern.txt
Agarwal
Aggarwal
Agrawal
$grep –f pattern.txt geekfile.txt
Syntax:
$grep -A[NumberOfLines(n)] [search] [file]
$grep -B[NumberOfLines(n)] [search] [file]
$grep -C[NumberOfLines(n)] [search] [file]
Example:
$grep -A1 learn geekfile.txt
Output:
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
(Prints the searched line along with the next n lines (here n = 1 (A1).)
(Will print each and every occurrence of the found line, separating each output by --)
(Output pattern remains the same for -B and -C respectively) Unix linux which one you choose. -- uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. Unix linux which one you choose. -- uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.