Question:
what is the difference between grep and fgrep?
bamaboy
2007-10-11 08:09:18 UTC
what is the difference between grep and fgrep?
Six answers:
rahul
2007-10-11 22:24:21 UTC
fgrep searches files for one or more pattern arguments. It does not use regular expressions; instead, it does direct string comparison to find matching lines of text in the input.



egrep works in a similar way, but uses extended regular expression matching (as well as the \< and \> metacharacters) as described in regexp. If you include special characters in patterns typed on the command line, escape them by enclosing them in apostrophes to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to egrep, put a backslash (\) in front of the character. It is usually simpler to use fgrep when you don't need special pattern matching.



grep is a combination of fgrep and egrep. If you do not specify either -E or -F, (or their long form equivalents, --extended-regexp or --fixed-strings), grep behaves like egrep, but matches basic regular expressions instead of extended ones. You can specify a pattern to search for with either the -e or -f option. If you specify neither option, grep (or egrep or fgrep) takes the first non-option argument as the pattern for which to search. If grep finds a line that matches a pattern, it displays the entire line. If you specify multiple input files, the name of the current file precedes each output line.





grep accepts all of the following options while egrep and fgrep accept all but the -E and -F options.





-A num

--after-context=num

displays num lines following each matched line.





Note:

When displaying lines of context (lines before or after matching lines), grep will not display any line more than once.





-a

--text

treats all input files as ASCII text files, even if a file would normally be treated as a binary file.



-B num

--before-context=num

displays num lines before each matched line.





Note:

When displaying lines of context (lines before or after matching lines), grep will not display any line more than once.





-b

precedes each matched line with its file block number.



--binary

treats all input files as binary files, even if a file would normally be treated as a text file.





Note:

On many Linux systems, this behavior is also available by using the -U option. However, MKS Toolkit uses -U for a different purpose. If you are writing scripts to run with both Linux and MKS Toolkit, you should use --binary exclusively on both systems.





--byte-offset

displays the byte offset with the input file before each line.





Note:

On many Linux systems, this behavior is also available by using the -b option. However, MKS Toolkit uses -b for a different purpose. If you are writing scripts to run with both Linux and MKS Toolkit, you should use --byte-offset exclusively on both systems.





-C [num]

--context=num

-num

displays num lines before and after each matched line. If num is omitted, grep displays two lines before and after each matching line.





Note:

When displaying lines of context (lines before or after matching lines), grep will not display any line more than once.





-c

displays only a count of the number of matched lines and not the lines themselves.



-d action

--directories=action

specifies how grep is to treat input files that are directories. action can be read, skip, or recurse. If action is read, all directories are treated as normal files. If action is skip, grep skips over directory files. If action is recurse, grep treats all files under the directory (and its subdirectories) as input files.



The -d recurse option (or --directories recurse) is the same as -r (or --recursive).



-E

--extended-regexp

causes grep to behave like egrep.



-e pattern

specifies one or more patterns for which grep is to search. You may indicate each pattern with a separate -e option character, or with newlines within pattern. For example, the following two commands are equivalent:



grep -e pattern_one -e pattern_two file

grep -e 'pattern_one

pattern_two' file



-F

--fixed-strings

causes grep to behave like fgrep.



-f patternfile

reads one or more patterns from patternfile. Patterns in patternfile are separated by newlines.



-G

--basic-regexp

causes grep, egrep. or fgrep to behave like the standard grep utility.



-h

--no-filename

does not display file names as prefixes when multiple input files are specified.



-i

ignores the case of the strings being matched.



-L

--files-without-match

lists only the file names of files that do not contain matching lines.



-l

lists only the file names of files that contain the matching lines.



-n

precedes each matched line with its file line number.



-q

suppresses output and simply returns appropriate return code.



-r

--recursive

when an input file is a directory, grep treats all files under that directory (and its subdirectories) as input files.



This option is identical to the -d recurse option (or --directories recurse).



-s

suppresses the display of any error messages for nonexistent or unreadable files.



-U[[[c][lb8oa]][p[lb8oa]]]

specifies the input format of any file missing the initial multiple-byte marker, the output format produced, or both.



When c is specified, the specifiers that follow it apply to the input consumed.



When p is specified, the specifiers that follow it apply to the output produced.



When neither c nor p are specified, the remaining -U specifiers apply to the input consumed.



When both c and p are specified, the remaining -U arguments apply to both input and output.



The remaining specifiers indicate the format of the characters read from input or written to output (as determined by c and p):



l little-endian 16-bit wide characters

b big-endian 16-bit wide characters

8 UTF-8 characters

a ASCII characters from the ANSI code page

o ASCII characters from the OEM code page



When multiple format specifiers can be associated with either c or p, the last appropriate one given on the command for each of c and p is used. For example:



-Ucoapl8



is the same as:



-Ucap8



When a p specifier is given without a c specifier and format specifiers are given before the p specifier, those format specifiers apply to the input. For example:



-Uopl



is the same as:



-Ucopl



When c or p is specified with no format specifies, little endian 16-bit wide characters are used by default for either input or output, as appropriate.



As an alternative to specifying formats for both input and output with the same -U option, you can specify the -U option multiple times. For example, the following are identical:



-Uca -Upb

-Ucapb





Note:

The -U specifiers are actually case-insensitive. For example, the following are all identical in their behavior:



-Ucl

-UcL

-UCl

-UCL





-u

--unix-byte-offset

when used with the --byte-offset option, displays byte offsets as UNIX-style byte offsets (that is, carriage return characters are ignored).



-V

--version

displays the version number of grep.



-v

--revert-match

displays all lines not matching a pattern.



-w

--word-rexexp

only match lines where the pattern matches a whole word. To be a whole word, the match must either be at the beginning of the line, or preceded by a non-word character. As well, the match must be either at the end of the line or followed by a non-word character. Words can be made up of letters, digits, and underscores (_). All other characters are considered non-word characters.



Neither fgrep nor grep -F support the -w and --word-rexexp options



-x

requires a string to match an entire line.





its Enough i think so ..
?
2016-09-28 03:32:43 UTC
Grep Vs Egrep
Berke
2016-02-09 00:02:35 UTC
This Site Might Help You.



RE:

what is the difference between grep and fgrep?
2016-03-13 12:07:52 UTC
grep- global regular expression and print Grep - Finds text within a file Egrep(expression grep)- Search a file for a pattern using full regular expressions fgrep(fixed-character grep) - Search a file for a fixed-character string hope i answered your question :)
?
2016-12-26 23:24:05 UTC
Fgrep Example
JKP
2007-10-11 08:22:30 UTC
GOOGLE IT.



This question is not hard.



Hint: "Fgrep" is "grep" with the -F flag set.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...