Grep egrep fgrep rgrep


From Linuxconfig.org

(Redirected from Fgrep)
Jump to: navigation, search

.. back to the list of Linux Commands

Contents

Name

grep, egrep, fgrep, rgrep - print lines matching a pattern

Synopsis - man page

grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]

Examples

grep

To begin this linux grep command line tutorial first we need to create a sample files to play with, run command below:

echo -e "Redhatlinux\nDebianLInux4.0\nPCLinuxOS\nOpenSUSE\nLinUxFEDORA\n\nMandriva" > file.grep_1
echo -e "MintLinux\nLinuxGYM1.0.3\nUbuntu\nKubuntu\nSlaxLiNuX\n\nKnoppix" > file.grep_2

As you already assume grep can be used to search string within a file. Lets search for word "linux":

grep linux *

Image:grep_search_string.gif

As everything else in Linux grep is also case sensitive, to ignore case we need to use grep with combination of -i option:

grep -i linux *

Image:grep_search_string_ignore_case.gif

grep option -h allows us to suppress to display file name and with -n option grep numbers lines within a file. --colour=auto highlights match:

grep -inh --colour=auto linux *

Image:grep_search_string_ignore_case_suppress_name_line_number.gif

If we do not want grep to output lines which does NOT contain lines with keyword linux we use -v option:

grep -iv linux *

Image:grep_search_string_print_opposite.gif

With -c grep can count string occurrences within files, so here the grep will print number of how many times keyword linux does NOT appear within both files:

grep -icv linux *

Image:grep_search_string_count_string_occurrences.gif

If we are looking for the name of the file which contains keyword "Ubuntu" we would use grep's -l option:

grep -l Ubuntu *

Image:grep_search_string_output_filename_only.gif

Grep with -x option will print exact occurrences only.

grep -ixc linux *

Image:grep_search_string_exact_occurrences.gif


Grep can even use a file which contain match pattern:

echo "LinUx" > grep_pattern.txt
grep -f grep_pattern.txt *

Image:grep_search_string_use_pattern_from_file.gif

System administrators will definitely appreciate following grep options when search log files. -B3 ( display 3 lines before match ) and -A3 ( display 3 lines after ) match. To make it even more readable --colour=auto can be used:

grep -B3 -A3 --colour=auto "command" /var/log/dmesg

Image:grep_lines_before_after_highlight_match.gif

Grep & regex

Grep and Regular Expressions ( regex ) This topic can definitely cover whole book but it would be shame to not show at least couple examples for grep and regular expressions. For example to make grep return only lines which contains digits, we use a command:

grep [0-9] *

Image:grep_return_lines_with_digits.gif

To count all empty lines within a file using grep we use command:

grep -ch ^$ file.grep_2

Image:grep_count_empty_line_within_file.gif

Let's see what line starts with "L" and ends with number ( ^ -> match beginning of the line, $ -> match end of the line):

grep ^L.*[0-9]$  *

Image:grep_beginning_end_line.gif

To make grep match only lines where "b" is a third character in the word we can use following command:

grep ..b  *

Image:grep_match_third_character.gif

egrep

egrep is extended version of grep or egrep is equal to grep -E. Egrep supports more regular expression patterns. Lets search for lines which contains at exactly two consecutive p characters:

egrep p{2} *
OR
grep pp *
OR
grep -E p{2} *

Image:egrep_exactly_two_characters.gif

lets get an output of egrep command with all lines which ends with "S" OR "A" :

egrep "S$|A$" *

Image:egrep_OR_functionality.gif

fgrep

fgrep is a faster version of grep which does not support regular expressions and therefore is considered to be faster. fgrep is equal to grep -F.

Simple proof that fgrep does not interpret regular expressions (regex):

fgrep linux$ *
egrep linux$ *
grep linux$ *

Image:fgrep_does_not_support_regex.gif

rgrep

rgrep is a recursive version of grep. Recursive in this case means that rgrep can recursively descend through directories as it greps for the specified pattern. rgrep is similar to grep -r.

mkdir -p dir/dir1
echo "Linux_rgrep" > dir/dir1/rgrep.txt
grep rgrep *
rgrep rgrep *
grep -r rgrep *

Image:rgrep_recursive_grep_example.gif

.. back to the list of Linux Commands

Personal tools
Linux Commands