“fgrep” stands for “Fixed-String Global Regular Expressions”. It looks for lines that match a pattern in the files given by the File parameter or in the standard input by default. If you put more than one file in the File parameter, the fgrep command shows the file that has the matched line. The fgrep command uses an algorithm that is quick and small, also search for strings that have a lot of metacharacters for regular expressions, such as $, *, [, |, (, ), and so on.
Command
$ fgrep [options] [-e pattern_list] pattern [file] |
Options
Here are details about some options.
Option | Details |
---|---|
-i | Ignore case differences in patterns and input data so that characters that only differ in case will match. |
-f | Obtain patterns from File, one per line. |
-e Pattern | Tells you about a pattern. It works like a simple pattern, but it is useful when the pattern starts with a minus sign (-). |
-h | Display the matched lines, but do not display the filenames. |
-c | Print out the number of matching lines in each file. |
-l | Only shows a list of filenames. |
-n | Show the lines that match and their line numbers. |
-v | This makes all the lines that do not fit the pattern print out. |
-w | Select only those lines containing matches that form whole words. |
-x | Displays lines that exactly match the pattern without any extra characters. |
-R | Read all files under each directory, recursively. |
Examples
- Now, let us see some examples.
fgrep -i linux linux_introduction.txt
fgrep -c Linux linux_introduction.txt
fgrep -w Linux linux_introduction.txt
fgrep -n Linux linux_introduction.txt
fgrep -l Linux *
fgrep -x Ubuntu linux_distributions.txt