grep
: Global Regular Expression print.
grep
is a powerful program used to find text patterns within files. It uses regular expressions (shortly regex) to match the pattern.
When grep
encounters a pattern in the file, it prints out the lines containing it. If no file is given, it will search recursively search the given pattern in the files in current directory.
|
|
- To show the line number in the output, use
-n
option:
|
|
grep
is case-sensitive.
- To ignore case-sensitive in grep, use
-i
option:
|
|
Use some special characters or regular expressions in grep:
^
: search at the beginning of the line.$
: search at the end of the line..
: search any character.
Some handy options for grep:
-v
: tellsgrep
to print only those lines that do not match the pattern.
Variants
grep has two variants:
- egrep : Extended grep.
- fgrep : fast grep.
Egrep
Egrep allows to use complicated regex.
For example, search any words that start with either I
or o
:
|
|
search any words starts in the range between i
to u
:
|
|