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.
grep pattern [file...]
grep <search-pattern> file.txt
- To show the line number in the output, use
-n
option:
grep -n <search-pattern> file.txt
grep
is case-sensitive.
- To ignore case-sensitive in grep, use
-i
option:
grep -i <search-pattern> file.txt
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
:
egrep '^(i/o)' file.txt
search any words starts in the range between i
to u
:
egrep '^[i-u]' file.txt